Advertisement
Guest User

Untitled

a guest
May 2nd, 2016
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. struct curl_slist *headers=NULL; // init to NULL is important
  2.  
  3. headers = curl_slist_append(headers, "Accept: application/json");
  4. headers = curl_slist_append(headers, "Content-Type: application/json");
  5. headers = curl_slist_append(headers, "charsets: utf-8");
  6.  
  7. curl = curl_easy_init();
  8. if(curl) {
  9. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  10. curl_easy_setopt(curl, CURLOPT_URL, "https://m360-prototype.herokuapp.com/sessions.json");
  11. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "username=testuser&password=12345");
  12.  
  13. curl_easy_setopt(curl, CURLOPT_HTTPGET,1);
  14. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
  15. res = curl_easy_perform(curl);
  16.  
  17. if(CURLE_OK == res) {
  18. char *ct;
  19. /* ask for the content-type */
  20. res = curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ct);
  21. if((CURLE_OK == res) && ct)
  22. printf("We received Content-Type: %sn", ct);
  23. }
  24. }
  25.  
  26. std::string data = "username=testuser&password=12345";
  27. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
  28. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
  29. curl_easy_setopt(curl, CURLOPT_POST, 1);
  30.  
  31. std::string data = "{"username":"testuser","password":"12345"}";
  32. curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); // for --insecure option
  33. curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
  34. curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, data.length());
  35. curl_easy_setopt(curl, CURLOPT_POST, 1);
  36.  
  37. CURLcode ret;
  38. CURL *hnd;
  39. struct curl_slist *slist1;
  40. std::string jsonstr = "{"username":"bob","password":"12345"}";
  41.  
  42. slist1 = NULL;
  43. slist1 = curl_slist_append(slist1, "Content-Type: application/json");
  44.  
  45. hnd = curl_easy_init();
  46. curl_easy_setopt(hnd, CURLOPT_URL, "http://u/r/l");
  47. curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
  48. curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, jsonstr.c_str());
  49. curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.38.0");
  50. curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
  51. curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
  52. curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
  53. curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
  54.  
  55. ret = curl_easy_perform(hnd);
  56.  
  57. curl_easy_cleanup(hnd);
  58. hnd = NULL;
  59. curl_slist_free_all(slist1);
  60. slist1 = NULL;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement