C ++ - how to send an HTTP request using Curlpp or libcurl

I would like to send a request to send http in C ++. It seems that libcurl (Curlpp) is the way to go.

Now, here is a typical request sending

http://abc.com:3456/handler1/start?<name-Value pairs> The name values pairs will have: field1: ABC field2: b, c, d, e, f field3: XYZ etc. 

Now I would like to know how to achieve the same using curlpp or libcurl. Code snippets really help.

+7
c ++ libcurl curlpp
source share
1 answer

I have no experience with Curlpp, but I did just that with libcurl.

You can set the destination URL using

 curl_easy_setopt(m_CurlPtr, CURLOPT_URL, "http://urlhere.com/"); 

POST values ​​are stored in a linked list β€” you must have two variables to hold the beginning and end of this list so that cURL can add a value to it.

 struct curl_httppost* beginPostList; struct curl_httppost* endPostList; 

Then you can add this post variable using

 curl_formadd(&beginPostList, &endPostList, CURLFORM_COPYNAME, "key", CURLFORM_COPYCONTENTS, "value", CURLFORM_END); 

Sending then works as follows

 curl_easy_setopt(m_CurlPtr, CURLOPT_POST, true); curl_easy_setopt(m_CurlPtr, CURLOPT_HTTPPOST, beginPostList); curl_easy_perform(m_CurlPtr); 

Hope this helps!

+3
source

All Articles