Skip to content

Instantly share code, notes, and snippets.

@ictlyh
Created November 30, 2016 09:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ictlyh/9902a24d8465bcd7049041930c8565a3 to your computer and use it in GitHub Desktop.
Save ictlyh/9902a24d8465bcd7049041930c8565a3 to your computer and use it in GitHub Desktop.
Demo of posting data to webpage via lib curl.
/*
* Demo of posting data to webpage via lib curl.
* Build: gcc curl-post.c -o curl-post -lcurl
* Run: ./curl-post
*/
#include <curl/curl.h>
#include <stdio.h>
int main(void) {
CURL *curl;
CURLcode res;
/* In windows, this will init the winsock stuff */
curl_global_init(CURL_GLOBAL_ALL);
/* get a curl handle */
curl = curl_easy_init();
if(curl) {
/* Set the URL that is about to receive our POST. */
curl_easy_setopt(curl, CURLOPT_URL, "http://baidu.com");
/* Now specify the POST data */
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "name=count&value=10000");
/* Perform the request, res will get the return code */
res = curl_easy_perform(curl);
/* Check for errors */
if(res != CURLE_OK)
fprintf(stdout, "curl_easy_perform() failed: %s\n",
curl_easy_strerror(res));
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_global_cleanup();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment