Cooler_

Rest Test 2 0d1n

Jan 26th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.44 KB | None | 0 0
  1. /*
  2. Cooler`s simple Rest test... ( http://en.wikipedia.org/wiki/Representational_state_transfer )
  3. i use this URL
  4. http://httpbin.org/
  5. to test  with libcurl in C...
  6.  
  7. i have other purposes to use...
  8.  
  9. other ways to test
  10.  
  11. test client:
  12. http://resttesttest.com/
  13.  
  14. with rails:
  15. $ rails new restweb && cd restweb
  16. $ rails generate scaffold post title:string body:string
  17. $ rails server
  18. goto http://127.0.0.1:3000/posts (.json)
  19.  
  20. to run: gcc -o code code.c -l curl; ./code
  21. contact: c00f3r [at] gmail [dot] com
  22. */
  23. #include <stdio.h>
  24. #include <curl/curl.h>
  25.  
  26. int main(void)
  27. {
  28.   CURL *curl;
  29.   CURLcode res;
  30.  
  31.   curl_global_init(CURL_GLOBAL_ALL);
  32.  
  33.   curl = curl_easy_init();
  34.   if(curl)
  35.   {
  36.     char* jsonObj = "{ \"name\" : \" coolerudos\" , \"pass\" : \"milk666\" }";
  37.  
  38.     struct curl_slist *headers = NULL;
  39.  
  40.     curl_slist_append(headers, "Accept: application/json");
  41.     curl_slist_append(headers, "Content-Type: application/json");
  42.     curl_slist_append(headers, "charsets: utf-8"); // maybe not cool use utf8...
  43.  
  44.     curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/put");
  45.     curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
  46.     curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  47.     curl_easy_setopt(curl, CURLOPT_POSTFIELDS, jsonObj);
  48.     curl_easy_setopt(curl, CURLOPT_USERAGENT, "hellblazer/0.666");
  49.  
  50.     res = curl_easy_perform(curl);
  51.     curl_easy_cleanup(curl);
  52.   }
  53.   curl_global_cleanup();
  54.   return 0;
  55. }
Advertisement
Add Comment
Please, Sign In to add comment