Languages
[Edit]
EN

Bash - send HTTP POST request with JSON body using curl

10 points
Created by:
Mark-Rotteveel
267

In this short article, we would like to show how to send HTTP POST request with JSON body/payload using curl under Bash.

Quick solution:

echo '{"name":"john"}' | curl -X POST -H "Content-Type: application/json" -d @- http://localhost:80/path/to/rest/api

Where:

  • -X POST indicates request method,
  • -H "Content-Type: application/json" informs server API about sent content type,
  • -d @- lets to use stdin and send them JSON data (-d @- is shortcut for --data-binary @-).

 

Alternative solution

We can send JSON payload from a file using the following command:

curl -X POST -H "Content-Type: application/json" -d @/path/to/data.json http://localhost:80/path/to/rest/api

Where:

  • -d @/path/to/data.json indicates a file that contains JSON to send inside the body of the POST request.

Example /path/to/data.json file content:

{
    "name": "john"
}
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join