47_postexample.c 666 B

12345678910111213141516171819202122232425
  1. #include <curl/curl.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4. size_t writefunc(void *ptr, size_t size, size_t nmemb, void *unused)
  5. {
  6. write(STDOUT_FILENO, ptr, size * nmemb);
  7. return size * nmemb;
  8. }
  9. #define URL "http://example.com"
  10. #define DATA "data=hellofromcurl"
  11. int main()
  12. {
  13. printf("Posting " DATA " to " URL "\n");
  14. CURL *curl = curl_easy_init();
  15. curl_easy_setopt(curl, CURLOPT_URL, URL);
  16. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
  17. // That's all you need to do POST queries, the output will be ignored by example.com though
  18. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, DATA);
  19. curl_easy_perform(curl);
  20. curl_easy_cleanup(curl);
  21. return 0;
  22. }