Shraddha K
Shraddha K

Reputation: 97

How to use libcurl for HTTP post?

I am new using libcurl. I am not understanding clearly how to use it for HTTP POST requests and how to check the result. How can I use it for this?

Upvotes: 8

Views: 12534

Answers (3)

SajithP
SajithP

Reputation: 587

This is to share my experience in development of REST clients using libcurl and I found a very easy way to find out the code snippets for ANY REST call using POSTMAN in MANY LANGUAGES. And it really worked for me!!! Install POSTMAN and the check there's a button called 'Code' on the right side (I used this on windows only, but should work for other OS as well as libcurl is portable across many operating systems)

If anyone wants to read the documentation and examples it could be found here. https://curl.haxx.se/docs/faq.html#What_is_cURL

CODE GENERATOR in POSTMAN

You can find the correct code snippets using libcurl for the languages supported in the left pane. C, C#, PHP, Python, Java, Java script etc...

Best thing is you can test your call from POSTMAN and at the same time can find the right code snippet and the curl command to use from command prompt as well (If you select cURL from left pane)

enter image description here

Upvotes: 0

kmkaplan
kmkaplan

Reputation: 18960

#include <curl/curl.h>
main()
{
  CURL *curl;
  curl_global_init(CURL_GLOBAL_ALL);
  curl = curl_easy_init();
  curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
  curl_easy_setopt(curl, CURLOPT_URL, "http://www.example.com/hello-world");
  curl_easy_setopt(curl, CURLOPT_POST, 1);
  curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "foo=bar&foz=baz");
  curl_easy_perform(curl);
  curl_easy_cleanup(curl);
}

Upvotes: 15

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72735

Refer the manual page for documentation the -d option. You can use that multiple times to pass different key,value pairs to the server. Once that works, use the --libcurl flag to see what it would look like if you're trying to use libcurl to manually do this in your application.

Upvotes: 8

Related Questions