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