8

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?

Flexo
  • 87,323
  • 22
  • 191
  • 272
Shraddha K
  • 97
  • 1
  • 1
  • 8
  • Did you check out: http://curl.haxx.se/libcurl/c/postit2.html Kind regards, Bo – Bo. May 18 '12 at 10:18
  • Yes I checked it but it is given for uploading a file I have to use it for submitting form I m not getting it clearly how it can be done. – Shraddha K May 19 '12 at 10:10

3 Answers3

15
#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);
}
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
8

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.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • I just want to submit a form using libcurl say i have to submit for acount creation on yahoo.com how should i do it? I dont want to upload a file but to submit a form. – Shraddha K May 19 '12 at 10:08
  • K the generated code was using POSTFIELDS. But I wanted to submit a html form. – Shraddha K May 19 '12 at 11:35
  • What exactly is the problem? The postfields as far as I can tell are the values you want to post. – Noufal Ibrahim May 21 '12 at 06:13
  • K it also can be used.But how to do it using the function curl_formadd() ? I read that this function is basically used to submit form data to server. Example is given for uploading a file.And I want to use it to fill the form and submit it. – Shraddha K May 21 '12 at 07:00
  • Why don't you try something and post back if you have a genuine problem? I haven't used `libcurl` myself and this is a suggestion based on my skimming of the documentation. – Noufal Ibrahim May 21 '12 at 07:09
  • The example sends both fields and "a file" so it shows how to use the API. – Daniel Stenberg May 22 '12 at 12:43
  • Actually i was little bit confused about when which enctype is used.Now i got.Thanks :) – Shraddha K May 23 '12 at 06:58
  • You should upvote/accept answers that you find useful. That's how Stack Overflow works. – Noufal Ibrahim May 23 '12 at 07:47
  • sORRY.I am new to stackoverflow.I really was not knowing this.Thanks! – Shraddha K May 24 '12 at 07:34
0

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

SajithP
  • 592
  • 8
  • 19