How do I post form data using Curl? [Curl/Bash Code]
To post form data with Curl, you can use one of two command-line parameters: -F (--form) or -d (--data). The -F command-line parameter sends form data with the multipart/form-data content type, and the -d command-line parameter sends form data with the application/x-www-form-urlencoded content type. In this Curl POST Form example, we are sending a form to the ReqBin echo URL. Click Run to execute the Curl POST Form request online and see the results. The Curl/Bash code was automatically generated for the Curl POST Form example.
Curl is a popular command-line tool used by programmers and administrators that allows you to send requests to the server, submit web forms, and upload files. Curl supports over 25+ protocols, including HTTP and HTTPS, has built-in support for web forms, SSL, user authentication, and HTTP Cookies. Curl works on Linux, Mac, Windows. It is one of the best tools you can find on the market for debugging network requests and API calls.
What is HTTP POST?
The HTTP POST method is one of nine standard Hypertext Transfer Protocol (HTTP) methods. The POST method requests the webserver to receive and process the data contained in the body of the POST message. The POST method is used to send data to the server, upload files and images, as well as for and send HTML forms. Unlike GET and HEAD requests, the HTTP POST requests may change the server state.
What is HTML Form?
HTML forms collect user input on HTML pages and submit it to a server for processing. For example, a website might display an HTML page with fields for a name and an email address to register a user. The user can enter the required information and then click the Submit button to send the data to the server for processing. HTML forms are created using the <form> tag.
To post a web form with Curl, you need to use the -d command line option and pass the form data as key/value pairs. By default, Curl sends an HTTP POST request and posts the provided form data with the application/x-www-form-urlencoded content type. You can use the-H command line parameter to post a form with a different content type and pass the desired data type there, for example, "Content-Type: multipart/form-data". The -X POST-command-line parameter is optional and can be omitted.
Curl can submit web forms in the same way as a browser so that on the server-side, it is impossible to distinguish from which client the form was submitted. To do this, you also need to specify the User-Agent name and HTTP cookies additionally.
Curl POST Form Syntax
The general form of the Curl command for submitting a web form using the -d command line option is as follows:
Curl POST Form Syntax Using -d Option
curl [URL] -d "key1=value1&key2=value2"
A more verbose version of the same request looks like this:
Curl POST Form Syntax (Verbose Version)
curl -X POST [URL]
-H "Content-Type: application/x-www-form-urlencoded"
-X, --request: HTTP method for communicating with the server.
-H, --header: HTTP headers to send to the server with POST request.
-d, --data: Data to be sent to the server using a POST request in the form of key/value pairs.
Curl POST Form with multipart/form-data Format
The -F command line option tells Curl to send HTML data to the server in multipart/form-data format:
Multipart/form-data is one of the most commonly used content types for sending binary data to the server. Multipart means that data is sent to the server in separate parts. Each of the components may have a different content type, file name, and data. The data are separated from each other by a boundary string. Curl sends a multipart request with a specially formatted POST message body as a series of "parts" separated by MIME boundaries.