I'm sorry for my simple question but I'm new using libcurl. I have the following code:
curl_easy_setopt(curl, CURLOPT_URL, "http://mypage.com/index.php?DataSourceId=1");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
res = curl_easy_perform(curl);
Everything works fine, so when curl_easy_perform(curl) is executed I get the JSON string in the console and I want to get this string in a variable.
Please anyone can help me ?
Any suggestion would be appreciated.
To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response.
To post JSON data using Curl, you need to set the Content-Type of your request to application/json and pass the JSON data with the -d command line parameter. The JSON content type is set using the -H "Content-Type: application/json" command line parameter. JSON data is passed as a string.
Reading data from a JSON file In the following program, we first make a root of the property tree type and load the JSON file into it. Then, we use the declare variables roll, name, class1 to store the values of “roll no”, “name” and “class” respectively. Thereafter, we display all the values.
curl_easy_setopt is used to tell libcurl how to behave. By setting the appropriate options, the application can change libcurl's behavior. All options are set with an option followed by a parameter.
You need to ask libcurl to write directly into your own memory buffer via the CURLOPT_WRITEDATA
option.
Please refer to the getinmemory example that precisely illustrates how to do that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With