Попытка создать запрос PUT c ++ с curl с определенным телом - PullRequest
0 голосов
/ 15 марта 2020

Я пытаюсь воссоздать этот запрос curl.

curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' --header 'Authorization: Basic securePasswordHash' -d '{"IconID": 54}' 'https://127.0.0.1:1233/api/v1/current/icon'

Эта команда работает.

Я пытаюсь перевести это в код C ++. Пока мой код выглядит так:

  std::string wHeader = ("Authorization: Basic " + wLCUEncoded);
    std::string strJson = "{\"IconID\" : 54}"; 
    CURL* curl;
    CURLcode res;
    curl_global_init(CURL_GLOBAL_DEFAULT);
    curl = curl_easy_init();
    headerlist = curl_slist_append(headerlist, "Accept: application/json");
    headerlist = curl_slist_append(headerlist, wHeader.c_str());
    if (curl) {
        curl_easy_setopt(curl, CURLOPT_URL, wUrl.c_str());
        curl_easy_setopt(curl, CURLOPT_HEADER, true);
        curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
        curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, strJson);
        res = curl_easy_perform(curl);
        curl_easy_cleanup(curl);

    }

Результат выглядит так:

{"errorCode":"BAD_REQUEST","httpStatus":400,"message":"Unknown argument '{\"IconID\" : 54}' for 'Icon'."}* Connection #0 to host 127.0.0.1 left intact

Какие-нибудь советы о том, что мне следует делать? Я пытался преобразовать strJson в фактический json объект, но это привело к странной ошибке строки:

{"errorCode":"BAD_REQUEST","httpStatus":400,"message":"Unknown argument '\u0002╠╠╠╠╠╠╠\u0010^J\u000Fe\u0001' for 'Icon'."}* Connection #0 to host 127.0.0.1 left intact

1 Ответ

0 голосов
/ 16 марта 2020

Решением моей проблемы было включение заголовков.

headerlist = curl_slist_append(headerlist, "Accept: application/json");
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
headerlist = curl_slist_append(headerlist, "charset: utf-8");
...