xref: /aosp_15_r20/external/curl/docs/libcurl/opts/CURLOPT_HTTPHEADER.md (revision 6236dae45794135f37c4eb022389c904c8b0090d)
1*6236dae4SAndroid Build Coastguard Worker---
2*6236dae4SAndroid Build Coastguard Workerc: Copyright (C) Daniel Stenberg, <[email protected]>, et al.
3*6236dae4SAndroid Build Coastguard WorkerSPDX-License-Identifier: curl
4*6236dae4SAndroid Build Coastguard WorkerTitle: CURLOPT_HTTPHEADER
5*6236dae4SAndroid Build Coastguard WorkerSection: 3
6*6236dae4SAndroid Build Coastguard WorkerSource: libcurl
7*6236dae4SAndroid Build Coastguard WorkerProtocol:
8*6236dae4SAndroid Build Coastguard Worker  - HTTP
9*6236dae4SAndroid Build Coastguard Worker  - SMTP
10*6236dae4SAndroid Build Coastguard Worker  - IMAP
11*6236dae4SAndroid Build Coastguard WorkerSee-also:
12*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_CUSTOMREQUEST (3)
13*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_HEADER (3)
14*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_HEADEROPT (3)
15*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_MIMEPOST (3)
16*6236dae4SAndroid Build Coastguard Worker  - CURLOPT_PROXYHEADER (3)
17*6236dae4SAndroid Build Coastguard Worker  - curl_mime_init (3)
18*6236dae4SAndroid Build Coastguard WorkerAdded-in: 7.1
19*6236dae4SAndroid Build Coastguard Worker---
20*6236dae4SAndroid Build Coastguard Worker
21*6236dae4SAndroid Build Coastguard Worker# NAME
22*6236dae4SAndroid Build Coastguard Worker
23*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HTTPHEADER - set of HTTP headers
24*6236dae4SAndroid Build Coastguard Worker
25*6236dae4SAndroid Build Coastguard Worker# SYNOPSIS
26*6236dae4SAndroid Build Coastguard Worker
27*6236dae4SAndroid Build Coastguard Worker~~~c
28*6236dae4SAndroid Build Coastguard Worker#include <curl/curl.h>
29*6236dae4SAndroid Build Coastguard Worker
30*6236dae4SAndroid Build Coastguard WorkerCURLcode curl_easy_setopt(CURL *handle, CURLOPT_HTTPHEADER,
31*6236dae4SAndroid Build Coastguard Worker                          struct curl_slist *headers);
32*6236dae4SAndroid Build Coastguard Worker~~~
33*6236dae4SAndroid Build Coastguard Worker
34*6236dae4SAndroid Build Coastguard Worker# DESCRIPTION
35*6236dae4SAndroid Build Coastguard Worker
36*6236dae4SAndroid Build Coastguard WorkerPass a pointer to a linked list of HTTP headers to pass to the server and/or
37*6236dae4SAndroid Build Coastguard Workerproxy in your HTTP request. The same list can be used for both host and proxy
38*6236dae4SAndroid Build Coastguard Workerrequests.
39*6236dae4SAndroid Build Coastguard Worker
40*6236dae4SAndroid Build Coastguard WorkerWhen used within an IMAP or SMTP request to upload a MIME mail, the given
41*6236dae4SAndroid Build Coastguard Workerheader list establishes the document-level MIME headers to prepend to the
42*6236dae4SAndroid Build Coastguard Workeruploaded document described by CURLOPT_MIMEPOST(3). This does not affect raw
43*6236dae4SAndroid Build Coastguard Workermail uploads.
44*6236dae4SAndroid Build Coastguard Worker
45*6236dae4SAndroid Build Coastguard WorkerThe linked list should be a fully valid list of **struct curl_slist** structs
46*6236dae4SAndroid Build Coastguard Workerproperly filled in. Use curl_slist_append(3) to create the list and
47*6236dae4SAndroid Build Coastguard Workercurl_slist_free_all(3) to clean up an entire list. If you add a header that is
48*6236dae4SAndroid Build Coastguard Workerotherwise generated and used by libcurl internally, your added header is used
49*6236dae4SAndroid Build Coastguard Workerinstead. If you add a header with no content as in 'Accept:' (no data on the
50*6236dae4SAndroid Build Coastguard Workerright side of the colon), the internally used header is disabled/removed. With
51*6236dae4SAndroid Build Coastguard Workerthis option you can add new headers, replace internal headers and remove
52*6236dae4SAndroid Build Coastguard Workerinternal headers. To add a header with no content (nothing to the right side
53*6236dae4SAndroid Build Coastguard Workerof the colon), use the form 'name;' (note the ending semicolon).
54*6236dae4SAndroid Build Coastguard Worker
55*6236dae4SAndroid Build Coastguard WorkerThe headers included in the linked list **must not** be CRLF-terminated,
56*6236dae4SAndroid Build Coastguard Workerbecause libcurl adds CRLF after each header item itself. Failure to comply
57*6236dae4SAndroid Build Coastguard Workerwith this might result in strange behavior. libcurl passes on the verbatim
58*6236dae4SAndroid Build Coastguard Workerstrings you give it, without any filter or other safe guards. That includes
59*6236dae4SAndroid Build Coastguard Workerwhite space and control characters.
60*6236dae4SAndroid Build Coastguard Worker
61*6236dae4SAndroid Build Coastguard WorkerThe first line in an HTTP request (containing the method, usually a GET or
62*6236dae4SAndroid Build Coastguard WorkerPOST) is not a header and cannot be replaced using this option. Only the lines
63*6236dae4SAndroid Build Coastguard Workerfollowing the request-line are headers. Adding this method line in this list
64*6236dae4SAndroid Build Coastguard Workerof headers only causes your request to send an invalid header. Use
65*6236dae4SAndroid Build Coastguard WorkerCURLOPT_CUSTOMREQUEST(3) to change the method.
66*6236dae4SAndroid Build Coastguard Worker
67*6236dae4SAndroid Build Coastguard WorkerWhen this option is passed to curl_easy_setopt(3), libcurl does not copy the
68*6236dae4SAndroid Build Coastguard Workerentire list so you **must** keep it around until you no longer use this
69*6236dae4SAndroid Build Coastguard Worker*handle* for a transfer before you call curl_slist_free_all(3) on the list.
70*6236dae4SAndroid Build Coastguard Worker
71*6236dae4SAndroid Build Coastguard WorkerUsing this option multiple times makes the last set list override the previous
72*6236dae4SAndroid Build Coastguard Workerones. Set it to NULL to disable its use again.
73*6236dae4SAndroid Build Coastguard Worker
74*6236dae4SAndroid Build Coastguard WorkerThe most commonly replaced HTTP headers have "shortcuts" in the options
75*6236dae4SAndroid Build Coastguard WorkerCURLOPT_COOKIE(3), CURLOPT_USERAGENT(3) and CURLOPT_REFERER(3). We recommend
76*6236dae4SAndroid Build Coastguard Workerusing those.
77*6236dae4SAndroid Build Coastguard Worker
78*6236dae4SAndroid Build Coastguard WorkerThere is an alternative option that sets or replaces headers only for requests
79*6236dae4SAndroid Build Coastguard Workerthat are sent with CONNECT to a proxy: CURLOPT_PROXYHEADER(3). Use
80*6236dae4SAndroid Build Coastguard WorkerCURLOPT_HEADEROPT(3) to control the behavior.
81*6236dae4SAndroid Build Coastguard Worker
82*6236dae4SAndroid Build Coastguard Worker# SPECIFIC HTTP HEADERS
83*6236dae4SAndroid Build Coastguard Worker
84*6236dae4SAndroid Build Coastguard WorkerSetting some specific headers causes libcurl to act differently.
85*6236dae4SAndroid Build Coastguard Worker
86*6236dae4SAndroid Build Coastguard Worker## Host:
87*6236dae4SAndroid Build Coastguard Worker
88*6236dae4SAndroid Build Coastguard WorkerThe specified hostname is used for cookie matching if the cookie engine is
89*6236dae4SAndroid Build Coastguard Workeralso enabled for this transfer. If the request is done over HTTP/2 or HTTP/3,
90*6236dae4SAndroid Build Coastguard Workerthe custom hostname is instead used in the ":authority" header field and
91*6236dae4SAndroid Build Coastguard WorkerHost: is not sent at all over the wire.
92*6236dae4SAndroid Build Coastguard Worker
93*6236dae4SAndroid Build Coastguard Worker## Transfer-Encoding: chunked
94*6236dae4SAndroid Build Coastguard Worker
95*6236dae4SAndroid Build Coastguard WorkerTells libcurl the upload is to be done using this chunked encoding instead of
96*6236dae4SAndroid Build Coastguard Workerproviding the Content-Length: field in the request.
97*6236dae4SAndroid Build Coastguard Worker
98*6236dae4SAndroid Build Coastguard Worker# SPECIFIC MIME HEADERS
99*6236dae4SAndroid Build Coastguard Worker
100*6236dae4SAndroid Build Coastguard WorkerWhen used to build a MIME email for IMAP or SMTP, the following document-level
101*6236dae4SAndroid Build Coastguard Workerheaders can be set to override libcurl-generated values:
102*6236dae4SAndroid Build Coastguard Worker
103*6236dae4SAndroid Build Coastguard Worker## Mime-Version:
104*6236dae4SAndroid Build Coastguard Worker
105*6236dae4SAndroid Build Coastguard WorkerTells the parser at the receiving site how to interpret the MIME framing.
106*6236dae4SAndroid Build Coastguard WorkerIt defaults to "1.0" and should normally not be altered.
107*6236dae4SAndroid Build Coastguard Worker
108*6236dae4SAndroid Build Coastguard Worker## Content-Type:
109*6236dae4SAndroid Build Coastguard Worker
110*6236dae4SAndroid Build Coastguard WorkerIndicates the document's global structure type. By default, libcurl sets it
111*6236dae4SAndroid Build Coastguard Workerto "multipart/mixed", describing a document made of independent parts. When a
112*6236dae4SAndroid Build Coastguard WorkerMIME mail is only composed of alternative representations of the same data
113*6236dae4SAndroid Build Coastguard Worker(i.e.: HTML and plain text), this header must be set to "multipart/alternative".
114*6236dae4SAndroid Build Coastguard WorkerIn all cases the value must be of the form "multipart/*" to respect the
115*6236dae4SAndroid Build Coastguard Workerdocument structure and may not include the "boundary=" parameter.
116*6236dae4SAndroid Build Coastguard Worker
117*6236dae4SAndroid Build Coastguard Worker##
118*6236dae4SAndroid Build Coastguard Worker
119*6236dae4SAndroid Build Coastguard WorkerOther specific headers that do not have a libcurl default value but are
120*6236dae4SAndroid Build Coastguard Workerstrongly desired by mail delivery and user agents should also be included.
121*6236dae4SAndroid Build Coastguard WorkerThese are `From:`, `To:`, `Date:` and `Subject:` among others and their
122*6236dae4SAndroid Build Coastguard Workerpresence and value is generally checked by anti-spam utilities.
123*6236dae4SAndroid Build Coastguard Worker
124*6236dae4SAndroid Build Coastguard Worker# SECURITY CONCERNS
125*6236dae4SAndroid Build Coastguard Worker
126*6236dae4SAndroid Build Coastguard WorkerBy default, this option makes libcurl send the given headers in all HTTP
127*6236dae4SAndroid Build Coastguard Workerrequests done by this handle. You should therefore use this option with
128*6236dae4SAndroid Build Coastguard Workercaution if you for example connect to the remote site using a proxy and a
129*6236dae4SAndroid Build Coastguard WorkerCONNECT request, you should to consider if that proxy is supposed to also get
130*6236dae4SAndroid Build Coastguard Workerthe headers. They may be private or otherwise sensitive to leak.
131*6236dae4SAndroid Build Coastguard Worker
132*6236dae4SAndroid Build Coastguard WorkerUse CURLOPT_HEADEROPT(3) to make the headers only get sent to where you
133*6236dae4SAndroid Build Coastguard Workerintend them to get sent.
134*6236dae4SAndroid Build Coastguard Worker
135*6236dae4SAndroid Build Coastguard WorkerCustom headers are sent in all requests done by the easy handle, which implies
136*6236dae4SAndroid Build Coastguard Workerthat if you tell libcurl to follow redirects
137*6236dae4SAndroid Build Coastguard Worker(CURLOPT_FOLLOWLOCATION(3)), the same set of custom headers is sent in
138*6236dae4SAndroid Build Coastguard Workerthe subsequent request. Redirects can of course go to other hosts and thus
139*6236dae4SAndroid Build Coastguard Workerthose servers get all the contents of your custom headers too.
140*6236dae4SAndroid Build Coastguard Worker
141*6236dae4SAndroid Build Coastguard WorkerStarting in 7.58.0, libcurl specifically prevents "Authorization:" headers
142*6236dae4SAndroid Build Coastguard Workerfrom being sent to other hosts than the first used one, unless specifically
143*6236dae4SAndroid Build Coastguard Workerpermitted with the CURLOPT_UNRESTRICTED_AUTH(3) option.
144*6236dae4SAndroid Build Coastguard Worker
145*6236dae4SAndroid Build Coastguard WorkerStarting in 7.64.0, libcurl specifically prevents "Cookie:" headers from being
146*6236dae4SAndroid Build Coastguard Workersent to other hosts than the first used one, unless specifically permitted
147*6236dae4SAndroid Build Coastguard Workerwith the CURLOPT_UNRESTRICTED_AUTH(3) option.
148*6236dae4SAndroid Build Coastguard Worker
149*6236dae4SAndroid Build Coastguard Worker# DEFAULT
150*6236dae4SAndroid Build Coastguard Worker
151*6236dae4SAndroid Build Coastguard WorkerNULL
152*6236dae4SAndroid Build Coastguard Worker
153*6236dae4SAndroid Build Coastguard Worker# %PROTOCOLS%
154*6236dae4SAndroid Build Coastguard Worker
155*6236dae4SAndroid Build Coastguard Worker# EXAMPLE
156*6236dae4SAndroid Build Coastguard Worker
157*6236dae4SAndroid Build Coastguard Worker~~~c
158*6236dae4SAndroid Build Coastguard Workerint main(void)
159*6236dae4SAndroid Build Coastguard Worker{
160*6236dae4SAndroid Build Coastguard Worker  CURL *curl = curl_easy_init();
161*6236dae4SAndroid Build Coastguard Worker
162*6236dae4SAndroid Build Coastguard Worker  struct curl_slist *list = NULL;
163*6236dae4SAndroid Build Coastguard Worker
164*6236dae4SAndroid Build Coastguard Worker  if(curl) {
165*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_URL, "https://example.com");
166*6236dae4SAndroid Build Coastguard Worker
167*6236dae4SAndroid Build Coastguard Worker    list = curl_slist_append(list, "Shoesize: 10");
168*6236dae4SAndroid Build Coastguard Worker    list = curl_slist_append(list, "Accept:");
169*6236dae4SAndroid Build Coastguard Worker
170*6236dae4SAndroid Build Coastguard Worker    curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
171*6236dae4SAndroid Build Coastguard Worker
172*6236dae4SAndroid Build Coastguard Worker    curl_easy_perform(curl);
173*6236dae4SAndroid Build Coastguard Worker
174*6236dae4SAndroid Build Coastguard Worker    curl_slist_free_all(list); /* free the list */
175*6236dae4SAndroid Build Coastguard Worker  }
176*6236dae4SAndroid Build Coastguard Worker}
177*6236dae4SAndroid Build Coastguard Worker~~~
178*6236dae4SAndroid Build Coastguard Worker
179*6236dae4SAndroid Build Coastguard Worker# HISTORY
180*6236dae4SAndroid Build Coastguard Worker
181*6236dae4SAndroid Build Coastguard WorkerUse for MIME mail added in 7.56.0.
182*6236dae4SAndroid Build Coastguard Worker
183*6236dae4SAndroid Build Coastguard Worker# %AVAILABILITY%
184*6236dae4SAndroid Build Coastguard Worker
185*6236dae4SAndroid Build Coastguard Worker# RETURN VALUE
186*6236dae4SAndroid Build Coastguard Worker
187*6236dae4SAndroid Build Coastguard WorkerReturns CURLE_OK if HTTP is supported, and CURLE_UNKNOWN_OPTION if not.
188