xref: /curl/docs/cmdline-opts/form.md (revision 0066d169)
1---
2c: Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
3SPDX-License-Identifier: curl
4Long: form
5Short: F
6Arg: <name=content>
7Help: Specify multipart MIME data
8Protocols: HTTP SMTP IMAP
9Mutexed: data head upload-file
10Category: http upload post imap smtp
11Added: 5.0
12Multi: append
13See-also:
14  - data
15  - form-string
16  - form-escape
17Example:
18  - --form "name=curl" --form "file=@loadthis" $URL
19---
20
21# `--form`
22
23For the HTTP protocol family, emulate a filled-in form in which a user has
24pressed the submit button. This makes curl POST data using the Content-Type
25multipart/form-data according to RFC 2388.
26
27For SMTP and IMAP protocols, this composes a multipart mail message to
28transmit.
29
30This enables uploading of binary files etc. To force the 'content' part to be
31a file, prefix the filename with an @ sign. To just get the content part from
32a file, prefix the filename with the symbol \<. The difference between @ and
33\< is then that @ makes a file get attached in the post as a file upload,
34while the \< makes a text field and just get the contents for that text field
35from a file.
36
37Read content from stdin instead of a file by using a single "-" as filename.
38This goes for both @ and \< constructs. When stdin is used, the contents is
39buffered in memory first by curl to determine its size and allow a possible
40resend. Defining a part's data from a named non-regular file (such as a named
41pipe or similar) is not subject to buffering and is instead read at
42transmission time; since the full size is unknown before the transfer starts,
43such data is sent as chunks by HTTP and rejected by IMAP.
44
45Example: send an image to an HTTP server, where 'profile' is the name of the
46form-field to which the file **portrait.jpg** is the input:
47
48    curl -F profile=@portrait.jpg https://example.com/upload.cgi
49
50Example: send your name and shoe size in two text fields to the server:
51
52    curl -F name=John -F shoesize=11 https://example.com/
53
54Example: send your essay in a text field to the server. Send it as a plain
55text field, but get the contents for it from a local file:
56
57    curl -F "story=<hugefile.txt" https://example.com/
58
59You can also instruct curl what Content-Type to use by using `type=`, in a
60manner similar to:
61
62    curl -F "web=@index.html;type=text/html" example.com
63
64or
65
66    curl -F "name=daniel;type=text/foo" example.com
67
68You can also explicitly change the name field of a file upload part by setting
69filename=, like this:
70
71    curl -F "file=@localfile;filename=nameinpost" example.com
72
73If filename/path contains ',' or ';', it must be quoted by double-quotes like:
74
75    curl -F "file=@\"local,file\";filename=\"name;in;post\"" \
76        https://example.com
77
78or
79
80    curl -F 'file=@"local,file";filename="name;in;post"' \
81        https://example.com
82
83Note that if a filename/path is quoted by double-quotes, any double-quote
84or backslash within the filename must be escaped by backslash.
85
86Quoting must also be applied to non-file data if it contains semicolons,
87leading/trailing spaces or leading double quotes:
88
89    curl -F 'colors="red; green; blue";type=text/x-myapp' \
90       https://example.com
91
92You can add custom headers to the field by setting headers=, like
93
94    curl -F "submit=OK;headers=\"X-submit-type: OK\"" example.com
95
96or
97
98    curl -F "submit=OK;headers=@headerfile" example.com
99
100The headers= keyword may appear more that once and above notes about quoting
101apply. When headers are read from a file, Empty lines and lines starting
102with '#' are comments and ignored; each header can be folded by splitting
103between two words and starting the continuation line with a space; embedded
104carriage-returns and trailing spaces are stripped.
105Here is an example of a header file contents:
106
107    # This file contain two headers.
108    X-header-1: this is a header
109
110    # The following header is folded.
111    X-header-2: this is
112     another header
113
114To support sending multipart mail messages, the syntax is extended as follows:
115
116- name can be omitted: the equal sign is the first character of the argument,
117
118- if data starts with '(', this signals to start a new multipart: it can be
119followed by a content type specification.
120
121- a multipart can be terminated with a '=)' argument.
122
123Example: the following command sends an SMTP mime email consisting in an
124inline part in two alternative formats: plain text and HTML. It attaches a
125text file:
126
127    curl -F '=(;type=multipart/alternative' \
128         -F '=plain text message' \
129         -F '= <body>HTML message</body>;type=text/html' \
130         -F '=)' -F '=@textfile.txt' ...  smtp://example.com
131
132Data can be encoded for transfer using encoder=. Available encodings are
133*binary* and *8bit* that do nothing else than adding the corresponding
134Content-Transfer-Encoding header, *7bit* that only rejects 8-bit characters
135with a transfer error, *quoted-printable* and *base64* that encodes data
136according to the corresponding schemes, limiting lines length to 76
137characters.
138
139Example: send multipart mail with a quoted-printable text message and a
140base64 attached file:
141
142    curl -F '=text message;encoder=quoted-printable' \
143         -F '=@localfile;encoder=base64' ... smtp://example.com
144
145See further examples and details in the MANUAL.
146