• Modern UX

    Edit and navigate faster in the terminal with Warp's IDE-like input editor.

  • Warp AI

    AI suggests what commands to run and learns from your documentation.

  • Agent Mode

    Delegate tasks to AI and use natural language on the command line.

  • Warp Drive

    Save and share interactive notebooks, workflows, and environment variables.

  • All Features

POST JSON Data With Curl

Thumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran Gul
Neeran Gul

Neeran Gul

Staff Site Reliability Engineer, Mozn

Updated: 7/26/2024

Published: 1/5/2023

About Terminus

The short answer

To send a HTTP request with a payload in the JSON format, you can use the curl command as follows:

$ curl -X POST -H 'Content-Type: application/json' -d '{"<key>":"<value>", ...}' <url>

Run in Warp

Where:

  • The -X flag is used to set the HTTP method of the request to POST.
  • The -H flag is used to set the Content-Type header of the HTTP request to application/json, which corresponds to the JSON media type.
  • The -d flag is used to specify the payload of the HTTP request.

For example:

$ curl -X POST -H 'Content-Type: application/json' -d '{"email":"user@example.com","password":"helloworld"}' http://example.com/login

Run in Warp

Note that as of curl version 7.82.0, you can also use the --json flag as follows:

$ curl --json '{"<key>":"<value>", ...}' <url>

Run in Warp

To learn more about sending HTTP POST requests in general, you can read our other article on how to send POST requests with cURL.

Easily retrieve this command using Warp’s AI Command Suggestions

If you’re using Warp as your terminal, you can easily retrieve this command using the Warp AI Command Suggestions feature:

Thumbnail for Thumbnail for Thumbnail for Thumbnail for

Entering send json data with curl in the AI Command Suggestions will prompt a curl command that can then be quickly inserted in your shell by doing CMD+ENTER .

Sending a JSON payload from a file

To send JSON data from a file instead of the command-line, you can use the following syntax:

$ curl -X POST -H 'Content-Type: application/json' -d @<file> <url>

Run in Warp

Where:

  • file is the path to the file containing the JSON payload.

For example, this command will send a request with a JSON payload loaded from the file named data.json located in the current working directory:

$ curl -X POST -H 'Content-Type: application/json' -d @data.json http://example.com

Run in Warp

Handling large JSON payloads

To send large JSON payloads as it, without any character conversion or modification, you can use the --data-binary flag as follows:

$ curl -X POST -H 'Content-Type: application/json' --data-binary @<file> <url>

Run in Warp

Properly formatting the JSON object

Checking the payload format

To make sure that the JSON payload is properly formatted, you can pipe it into the jq command as follows:

$ echo '{"<key>":"<value>", ...}' | jq
$ cat <file> | jq

Run in Warp

The jq command will then parse the data, and either write the JSON object back to the terminal if valid, or output an error message otherwise.

For example:

$ echo '{"hello":"world"}' | jq
{
  "hello": "world"
}
$ echo '{"hello":world}' | jq
jq: parse error: Invalid numeric literal at line 1, column 15

Run in Warp

Escaping special characters in JSON

In JSON, certain characters such as double quotes (") or backslashes (\) need to be escaped to ensure that the data is correctly formatted and parsed. To escape these characters, you can prepend them with a backslash character.

For example, in the following request, the string h3\o" needs to be escaped as follows:

$ curl -X POST -H 'Content-Type: application/json' -d '{"email":"user@example.com","password":"h3\\o\""}' http://example.com/login

Run in Warp

Specifying the payload’s charset

To prevent encoding issues between the client and the server, you can specify the character set of the HTTP request through the Content-Type header as follows:

$ curl -X POST -H 'Content-Type: application/json; charset=UTF-8' -d <payload> <url>

Run in Warp

Sending authenticated requests

It may sometimes happen that when sending a request to a protected endpoint, the server responds with an HTTP 401 Unauthorized. This means that the request must be authenticated using an access token, usually generated in response to a login request.

To send an authenticated HTTP request containing an access token, you can specify the Authorization header as follows:

$ curl -X POST \
-H 'Authorization: Bearer <token>' \
-H 'Content-Type: application/json' \
-d '{"<key>":"<value>"}' \
<url>

Run in Warp

For example:

$ curl -X POST \
-H 'Authorization: Bearer a8JxldMFxA' \
-H 'Content-Type: application/json' \
-d '{"":""}' \
http://example.com/

Run in Warp

Written by

Thumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran Gul
Neeran Gul

Neeran Gul

Staff Site Reliability Engineer, Mozn

Filed Under

Related Articles

Bash If Statement

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

Bash
Thumbnail for Gabriel ManricksThumbnail for Gabriel ManricksThumbnail for Gabriel ManricksThumbnail for Gabriel Manricks
Gabriel Manricks

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.

Bash

Use Cookies With cURL

Learn how to store and send cookies using files, hard-coded values, environment variables with cURL.

Bash

Loop Through Files in Directory in Bash

Learn how to iterate over files in a directory linearly and recursively using Bash and Python.

BashPython
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

How To Use sudo su

A quick overview of using sudo su

LinuxUnixBash
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

Generate, Sign, and View a CSR With OpenSSL

Learn how to generate, self-sign, and verify certificate signing requests with `openssl`.

BashLinuxUnix
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

How to use sudo rm -rf safely

We'll help you understand its components

BashLinuxUnix
Thumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran GulThumbnail for Neeran Gul
Neeran Gul

How to run chmod recursively

Using -R is probably not what you want

LinuxBashUnix
Thumbnail for Brett TerpstraThumbnail for Brett TerpstraThumbnail for Brett TerpstraThumbnail for Brett Terpstra
Brett Terpstra

Run Bash Shell In Docker

Start an interactive shell in Docker container

DockerBash
Thumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan LudosanuThumbnail for Razvan Ludosanu
Razvan Ludosanu

Curl Post Request

Use cURL to send data to a server

BashUnixLinux
Thumbnail for Zev StravitzThumbnail for Zev StravitzThumbnail for Zev StravitzThumbnail for Zev Stravitz
Zev Stravitz

Reading User Input

Via command line arguments and prompting users for input

BashLinuxUnix
Thumbnail for Amit JotwaniThumbnail for Amit JotwaniThumbnail for Amit JotwaniThumbnail for Amit Jotwani
Amit Jotwani

Bash Aliases

Create an alias for common commands

BashLinuxUnix
Thumbnail for Brett TerpstraThumbnail for Brett TerpstraThumbnail for Brett TerpstraThumbnail for Brett Terpstra
Brett Terpstra

Trusted by hundreds of thousands of professional developers

Download Warp to get started

Download for Mac
Request demo
Thumbnail for nullThumbnail for nullThumbnail for nullThumbnail for null