Send POST Request using Curl

Send POST Request using Curl

When it comes to interacting with web services and APIs, sending HTTP requests is a fundamental task. Among the many tools available for this purpose, Curl stands out as a powerful and versatile command line tool that allows sending various types of HTTP requests. HTTP POST requests are used to submit data to a specified resource, such as sending form data to a server or creating a new resource on an API endpoint. This tutorial explains how to send a POST request using Curl.

Simple POST request

To perform a simple POST request, use the following command:

curl -X POST -d "name=John&age=20" https://httpbin.org/post

In this command:

  • -X - specifies HTTP request method (e.g. POST).
  • -d - specifies request payload.

In this example, the response you will receive will be formatted in JSON, something like this:

{
  "args": {},
  "data": "",
  "files": {},
  "form": {
    "age": "20",
    "name": "John"
  },
  "headers": {
    "Accept": "*/*",
    "Content-Length": "16",
    "Content-Type": "application/x-www-form-urlencoded",
    "Host": "httpbin.org",
    "User-Agent": "curl/7.81.0",
    "X-Amzn-Trace-Id": "Root=1-6416c45e-47a4828a557d5fca764271d6"
  },
  "json": null,
  "origin": "XXX.XXX.XXX.XXX",
  "url": "https://httpbin.org/post"
}

JSON data

To send JSON data in an HTTP POST request using Curl, use the -d option to specify the data to send and set the appropriate headers using -H option.

curl -X POST -H "Content-Type:application/json" -d "{\"name\":\"John\"}" https://httpbin.org/post

Save output

To save the output of the POST request to a file, you can use the -o option:

curl -X POST -d "name=John&age=20" -o "response.json" https://httpbin.org/post

Leave a Comment

Cancel reply

Your email address will not be published.