如何在CPP中将JSON对象作为PUT请求发送? (请阅读说明)

时间:2020-07-20 02:57:32

标签: c++ json curl put http-put

这正在运行,但是当我使用Wireshark嗅探数据时,整个JSON对象被读取为KEY,并且在服务器端VALUE显示为空。如何格式化JSON对象,以便可以正确解析键和值?

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <json/value.h>
#include <jsoncpp/json/json.h>

using namespace std;
int main(int argc, char const *argv[]){
CURL *curl;
CURLcode res;
long http_code;
char* jsonObj ="{\"methodata\":[{\"methodname\":\"John\", \"projectname\":\"Doe\"}]}";

curl = curl_easy_init();
curl_global_init(CURL_GLOBAL_ALL);

if(curl){

    curl_easy_setopt(curl, CURLOPT_URL, "http://localhost:9090");
    curl_easy_setopt(curl, CURLOPT_HTTPAUTH, (long)CURLAUTH_BASIC);
    curl_easy_setopt(curl, CURLOPT_USERPWD, "root:");

    curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
    
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);


    res = curl_easy_perform(curl);

    if (res != CURLE_OK){
        fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res) );
    }

    //printf("\nget http return code\n");
    //curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
    //printf("http code: %lu\n", http_code );


    curl_easy_cleanup(curl);
    curl_global_cleanup();
    
    }
return 0;
}

0 个答案:

没有答案