How do I POST a buffer of JSON using libcurl?

后端 未结 4 1661
慢半拍i
慢半拍i 2021-02-02 15:08

My C++ program currently invokes curl through a pipe (popen(\"curl ...\")) to POST a file of JSON data to a web server. This has obvious performance limitations due

4条回答
  •  离开以前
    2021-02-02 15:16

    You can use CURLOPT_POSTFIELDS:

    CURL *curl = curl_easy_init();
    
    curl_easy_setopt(curl, CURLOPT_URL, "http://example.com/api/endpoint");
    curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"hi\" : \"there\"}");
    
    curl_easy_perform(curl);
    

    Since CURLOPT_POSTFIELDS does not modify the payload in any way, it is very convenient for POSTing JSON data. Also note that, when CURLOPT_POSTFIELDS is supplied, it automatically enables CURLOPT_POST so there is no need to provide CURLOPT_POST in the request.

提交回复
热议问题