JSON requests in C using libcurl

前端 未结 7 910
孤独总比滥情好
孤独总比滥情好 2020-12-08 12:04

I\'m defining a PUT request with a JSON request body using libcurl in C.

This how I\'m doing it:

    sprintf(jsonObj, \"\\\"name\\\" : \\\"%s\\\", \\         


        
7条回答
  •  生来不讨喜
    2020-12-08 12:50

    Had this same problem when posting data to node-red with json parser.
    Solution for me was to treat string as a HTML

    #include 
    
    int main (int argc, char *argv[]) {
        CURL *curl;
        CURLcode res;
    
        curl_global_init(CURL_GLOBAL_ALL);
        curl = curl_easy_init();
        if (curl == NULL) {
            return 128;
        }
        struct curl_slist *headers = NULL;
        curl_slist_append(headers, "Content-Type: application/json");
        curl_easy_setopt(curl, CURLOPT_URL, #yourURL);
        curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST");
        curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "age=42&sex=male");
    
        res = curl_easy_perform(curl);
    
        curl_easy_cleanup(curl);
        curl_global_cleanup();
        return res;
    }
    

    Hope someone will find it helpful.

提交回复
热议问题