尝试使用具有特定主体的curl创建c ++ PUT请求

问题描述 投票:-1回答:1

我想重新创建的是这个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
c++ json curl httprequest put
1个回答
0
投票
我的问题的解决方案是包括标题。

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");

© www.soinside.com 2019 - 2024. All rights reserved.