Curl Post Request

Zev Stravitz
Zev StravitzSoftware Engineer II, Rutter
Published: August 13, 2024

cURL (curl) is an open source command-line tool used for transferring data over a network. POST is an HTTP request method which is used to send data to the server. (Read more on POST requests here.)

The format of a POST request with curl is: curl -X POST options] URL].

An example POST request to submit a todo list task to a REST API might look like:

Bash
curl -H 'Content-Type: application/json' \
      -d '{ "title":"foo","body":"bar", "id": 1}' \
      -X POST \
      https://example.com/posts

Three options provided here are:

  • -X was used to provide the HTTP method we use to send the request. In this case, the method is POST
  • -d was used to provide the associated data in the body of the HTTP request
  • -H was used to specify headers for the request

Each of these options also have associated aliases. For example, for -X you can instead provide --request, and for -H you can provide --headers.

These are just some of the possible options, but there are many others, enumerated below.

Common POST Request Operations

Sending Form Data

There are two common ways to send form data with POST requests, each with different content-types, demonstrated below:

Bash
# multipart/form-data
## Text Field
curl -H "Content-type: multipart/form-data" \
     -F title=foo \
     -F body=bar \
     -F id="1" \
     -X POST \
     https://example.com/posts

## Image Upload
curl -H "Content-type: multipart/form-data" \
     -F profile=@avatar.jpg \
     https://example.com/avatar.cgi

# application/x-www-form-urlencoded
curl -H "Content-type: application/x-www-form-urlencoded" \
     -d "title=foo" \
     -d "body=bar" \
     -d "id=1" \
     -X POST \
     https://example.com/posts

As we can see from the image, to use multipart/form-data content-type, we send data with the -F (or --form option). To use application/x-www-form-urlencoded content-type, we use the -d (or --data) option.

Generally speaking, multipart/form-data is more often used to send binary data like images, while application/x-www-form-urlencoded is used to send text data.

By default, HTML forms will send data using the content-type of application/x-www-form-urlencoded when submitted.

Sending Image Data

To send image data in a POST request, include the image in the form data by prefixing the filename with an @ symbol as follows:

Bash
curl -F profile=@avatar.jpg \
     https://example.com/avatar.cgi

The @ symbol forces the content part of the form to be a file, which is then uploaded to the server.

Alternatively, we could base64 encode the image and send it as a field in a form field or JSON body field of your request, but this is far less ergonomic:

Bash
curl -H 'Content-Type: application/json' \
     -d  '{"image" : "'"$(base64 ~/avatar.jpg)"'"}' \
     -X POST \
     https://example.com/avatar.cgi

Common Gotchas

  • Argument names are case sensitive. For example, -F corresponds to the argument for form data, while -f is a flag indicating whether we want to fail fast with no output
  • When passing content of type application/json, be sure to wrap the JSON body in single quotes - e.g. -d "{ "title":"foo" }" would be invalid
  • You can only use breaklines if you include \ a backslash character. There can be no trailing spaces after the backslash

cURL POST Request Arguments

Arguments available for the options field of the curl POST format (curl -X POST options] URL]) are enumerated below:

Short OptionLong OptionArgument(s)Purpose
-X--requests<method> (use \"POST\" or POST requests)Specify the request method to use when communicating with an HTTP server
-b--cookie<data|filename>Specify which file to write all cookies after completed operation
-c--cookie-jar<filename>
-d--data<data>Send data in POST request
-f--failFail fast with no output on server errors
-F--form<name=content>Form data for content-type *application/x-www-form-urlencoded*
-H--header<header/@file>Header to include in the request
-i--includeInclude HTTP headers in the output
-l--headFetch the headers only
-k--insecureSkip verification that connection is secure
-L--locationIf a redirect is received (3XX), force curl to redo the request on the new address
-o--output<file>Write output to file instead of stdout
-O--remote-nameWrite output to a local file named like the remote file
-s--silentDo not show progress meter or error messages
-v--verboseMake curls verbose during operation. Useful for debugging
-w--write-out<format>Make curl display information on stdout after a completed transfer. See possible variables to include in output [here](https://curl.se/docs/manpage.html#-w)

Read more on the official curl docs.

Written by
Zev Stravitz
Zev StravitzSoftware Engineer II, Rutter
Filed under

Related articles


Bash Comments

Comments will help make your scripts more readable

Reading User Input

Via command line arguments and prompting users for input

Bash If Statement

Learn how to use the if statement in Bash to compare multiple values and expressions.

Bash While Loop

Learn how to use and control the while loop in Bash to repeat instructions, and read from the standard input, files, arrays, and more.

Upload Files With curl

Learn how to upload a file to FTP, SFTP servers, Artifactory, and AWS S3 using the curl command.

How To Copy A Directory In Linux

Learn how to copy directories and their content in Linux using the cp command with options like -r for recursive copying, -i for interactive mode, and -a for preserving attributes.

Create Groups In Linux

Learn how to manually and automatically create and list groups in Linux.

How to Check the Size of Folders in Linux

Learn how to output the size of directories and subdirectories in a human-readable format in Linux and macOS using the du command.

Count Files in Linux

Learn how to count files and folders contained in directories and subdirectories in Linux using the ls, find, and wc commands.

List Open Ports in Linux

Learn how to output the list of open TCP and UDP ports in Linux, as well as their IP addresses and ports using the netstat command.

Format Command Output In Linux

Learn how to filter and format the content of files and the output of commands in Linux using the awk command.

Run Bash Shell In Docker

Start an interactive shell in Docker container