3

Sending POST request using libcurl C++. I have tried almost all combination except the right one which I could not figure out.

curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE); /*Not Recommended to use but since certificates are not available this is a workaround added*/ curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, FALSE); /*Not Recommended to use but since certificates are not available this is a workaround added*/ curl_easy_setopt(curl, CURLOPT_HTTPPOST, TRUE); //CURLOPT_POST does not work as well curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sJson); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bytesCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, bytesCallback); curl_easy_setopt(curl, CURLOPT_HEADERDATA, &headBuffer); 

sJson is a std::string which has the body that is created by pb2json.

I cannot figure out why the body is not sent ? Is there some API i am missing if libcurl, any lead is appreciated !

8
  • body or header? Commented Jul 5, 2017 at 10:48
  • Body is not sent as the server says Received login request with no body Commented Jul 5, 2017 at 10:49
  • HTTPPOST sends a multipart message. You want CURLOPT_POST instead. Commented Jul 5, 2017 at 10:52
  • Even CURLOPT_POST does not work Commented Jul 5, 2017 at 10:53
  • Looks like you have written custom headers too. I use CURLOPT_HTTPHEADER to set custom headers: curl_slist *header_list = ::curl_slist_append(nullptr, header.data()); ::curl_easy_setopt(m_curl_handle, CURLOPT_HTTPHEADER, header_list); Commented Jul 5, 2017 at 10:55

1 Answer 1

5

I would prefer using custom request here CURLOPT_CUSTOMREQUEST below is the code snippet that works fine! When you use custom requests nothing implies and you have to explicitly define the CURLOPT_HTTPHEADER

Use a list here, for brevity I have used the minimal code here.

Also when you pass sJson pass it as a c type string using c_str() and remember to use +1 while passing the content length (which I somehow missed initially) as in C, strings are just char arrays which, by convention, end with a NULL byte.

struct curl_slist* slist = NULL; slist = curl_slist_append(slist, "Content-Type: application/json"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, sJson.c_str()); /* data goes here */ curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, sJson.length() + 1); /* data goes here */ 

EDIT: Use of curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "POST"); might create problems in redirection, use it according to your use case.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd recommend removing the CURLOPT_CUSTOMREQUEST, "POST") though, as that will cause headaches if you later want this to follow redirects etc, and it doesn't add anything in the first case. CURLOPT_POSTFIELDS implies POST.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.