Retrieving response text from curl in cpp

user2769274

we need to receive response string from curl in CPP ,I tried following options but nothing worked.js uses xhr.responseText for this.I need to do in cpp.

 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    size_t realsize = size * nmemb;
    const char* sp = static_cast<const char*>(contents);
    readBuffer.append(sp, realsize);
    return realsize;
 }

CURLcode res;
char* http_error= new char[100];

readBuffer.clear();
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
CURLcode code(CURLE_FAILED_INIT);
code = curl_easy_perform(curl);
cout <<  "Curl response msg CURLOPT_WRITEDATA: "<<curl_easy_strerror(code)<< " respose :"<<readBuffer;


res=curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code) ;
cout <<  "Curl response msg: "<< curl_easy_strerror(res);
huu

Change your WriteCallback function to this:

size_t WriteCallback(void* contents, size_t size, size_t nmemb, std::string* userp) {
    userp->append((char*) contents, size * nmemb);
    return size * nmemb;
}

Remember, you are passing in &readBuffer as the CURL_WRITEDATA option. This shows up as the fourth parameter in the WriteCallback. Since the type of &readBuffer is std::string*, you can use that as the signature in your callback.

Additionally, since it's a std::string* and not a std::string, you have to access append through the pointer, hence the -> instead of .. After curl_easy_perform, readBuffer should hold the response from the curl request.

To get the response code, you can grab that after making the request:

long http_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Retrieving the text from an edittext

From Dev

Retrieving the response body from an HttpClientResponse

From Dev

Retrieving value from ajax response

From Dev

Retrieving value from ajax response

From Dev

No response from cURL function

From Dev

cURL is retrieving encoded HTML from Pirate Bay

From Dev

cURL is retrieving encoded HTML from Pirate Bay

From Dev

PHP cURL retrieving response headers AND body in a single request in case of request?

From Dev

Retrieving a value from a text file

From Dev

Qt - retrieving text from QTextBrowser

From Dev

Retrieving text from a tag with jquery

From Dev

Retrieving JSON response from local database

From Java

Retrieving a few fields from large response object

From Dev

RestSharp - Retrieving Authorization token from POSTed response

From Dev

Retrieving all number row from JSON response

From Dev

Retrieving String from JAX-RS Response

From Dev

Retrieving image data URi from HTTP response

From Dev

Error in retrieving response from firebase cloud function

From Dev

Create variables from Curl response

From Dev

Cant get response error text in Curl request

From Dev

Retrieving certain strings from a text file in Java

From Dev

Retrieving Arabic text from MSSQL NVarchar column

From Dev

Retrieving text State from ServiceNow REST API

From Dev

Retrieving coordinates from a text file with Python

From Dev

retrieving the text value from 2 textareas

From Dev

Issue Retrieving JSON Content from URL in PHP with Curl

From Dev

Retrieving response string from Async. web request

From Dev

Retrieving dynamic JSONObject value from server response in servlets

From Dev

How to get the response from URL without cURL?

Related Related

HotTag

Archive