httpcustomheader.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. /* <DESC>
  23. * HTTP request with custom modified, removed and added headers
  24. * </DESC>
  25. */
  26. #include <stdio.h>
  27. #include <curl/curl.h>
  28. int main(void)
  29. {
  30. CURL *curl;
  31. CURLcode res;
  32. curl = curl_easy_init();
  33. if(curl) {
  34. struct curl_slist *chunk = NULL;
  35. /* Remove a header curl would otherwise add by itself */
  36. chunk = curl_slist_append(chunk, "Accept:");
  37. /* Add a custom header */
  38. chunk = curl_slist_append(chunk, "Another: yes");
  39. /* Modify a header curl otherwise adds differently */
  40. chunk = curl_slist_append(chunk, "Host: example.com");
  41. /* Add a header with "blank" contents to the right of the colon. Note that
  42. we're then using a semicolon in the string we pass to curl! */
  43. chunk = curl_slist_append(chunk, "X-silly-header;");
  44. /* set our custom set of headers */
  45. curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
  46. curl_easy_setopt(curl, CURLOPT_URL, "localhost");
  47. curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  48. res = curl_easy_perform(curl);
  49. /* Check for errors */
  50. if(res != CURLE_OK)
  51. fprintf(stderr, "curl_easy_perform() failed: %s\n",
  52. curl_easy_strerror(res));
  53. /* always cleanup */
  54. curl_easy_cleanup(curl);
  55. /* free the custom headers */
  56. curl_slist_free_all(chunk);
  57. }
  58. return 0;
  59. }