xref: /curl/docs/examples/sepheaders.c (revision 53b4dfe4)
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 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  * SPDX-License-Identifier: curl
22  *
23  ***************************************************************************/
24 /* <DESC>
25  * Simple HTTP GET that stores the headers in a separate file
26  * </DESC>
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 
31 #include <curl/curl.h>
32 
write_data(void * ptr,size_t size,size_t nmemb,void * stream)33 static size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
34 {
35   size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
36   return written;
37 }
38 
main(void)39 int main(void)
40 {
41   CURL *curl_handle;
42   static const char *headerfilename = "head.out";
43   FILE *headerfile;
44   static const char *bodyfilename = "body.out";
45   FILE *bodyfile;
46 
47   curl_global_init(CURL_GLOBAL_ALL);
48 
49   /* init the curl session */
50   curl_handle = curl_easy_init();
51 
52   /* set URL to get */
53   curl_easy_setopt(curl_handle, CURLOPT_URL, "https://example.com");
54 
55   /* no progress meter please */
56   curl_easy_setopt(curl_handle, CURLOPT_NOPROGRESS, 1L);
57 
58   /* send all data to this function  */
59   curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_data);
60 
61   /* open the header file */
62   headerfile = fopen(headerfilename, "wb");
63   if(!headerfile) {
64     curl_easy_cleanup(curl_handle);
65     return -1;
66   }
67 
68   /* open the body file */
69   bodyfile = fopen(bodyfilename, "wb");
70   if(!bodyfile) {
71     curl_easy_cleanup(curl_handle);
72     fclose(headerfile);
73     return -1;
74   }
75 
76   /* we want the headers be written to this file handle */
77   curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, headerfile);
78 
79   /* we want the body be written to this file handle instead of stdout */
80   curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, bodyfile);
81 
82   /* get it! */
83   curl_easy_perform(curl_handle);
84 
85   /* close the header file */
86   fclose(headerfile);
87 
88   /* close the body file */
89   fclose(bodyfile);
90 
91   /* cleanup curl stuff */
92   curl_easy_cleanup(curl_handle);
93 
94   return 0;
95 }
96