How To Send cURL POST Requests?

by mostafa Mar 13, 2024

To send a curl post request, we can use the following syntax:

curl -X POST \
     -H "Headers" \
     -d "Body" \
     https://httpbin.dev/post

This snippet shows the basic elements of a curl post data request:

-X: Specifies the request HTTP method, POST in this case.
-H: For adding request headers, which are often required for POST requests.
-d: For adding the cURL post body, which is where your payload goes (JSON, XML, or form data).

Common cURL POST Request Examples

Let's go through some typical use cases for sending a curl post request.

cURL POST Request with JSON

To pass a JSON body in a cURL POST request with JSON, use the -d option followed by the data object:

curl -X POST -H "Content-Type: application/json" -d '{"key1": "value1", "key2": "value2"}' https://httpbin.dev/post

Here, we modify the request Content-Type header and include the cURL request body in JSON format. The server responds with the parsed request:

{
  "args": {},
  "headers": {
    ....
    "Content-Type": [
      "application/json"
    ],
  },
  "method": "POST",
  "data": "{\"key1\": \"value1\", \"key2\": \"value2\"}",
  "json": {
    "key1": "value1",
    "key2": "value2"
  }
}

cURL POST Request with XML

You can also send a cURL POST request with XML by changing the header and body format:

curl -X POST \
     -H "Content-Type: application/xml"
     -d "<?xml version="1.0" encoding="UTF-8"?><product><id>1</id><name type="title">Box of Chocolate Candy</name></product>"
     https://httpbin.dev/post

This approach is common for SOAP-based or legacy APIs that require XML payloads.

Sending Files with cURL POST

To make cURL POST files, we use the -F flag to send file data as multipart/form-data:

curl -X POST -F "file=@/file/path/file.img" https://httpbin.dev/post

Note: If you're uploading multiple files, use multiple -F flags. This is useful for form-based file upload endpoints.

Sending Form POST Requests

Form POST requests are commonly required to retrieve tokens or data behind forms. Let's replicate this form example using cURL POST requests:

curl -X POST \
     -d 'custname=Scrapfly&custtel=&custemail=&size=small&topping=cheese&delivery=13%3A45&comments=' \
     https://httpbin.dev/post

This is especially useful when interacting with APIs or web apps that use forms to receive input.

Authenticate cURL POST Requests

Let's make cURL POST requests support basic HTTP authentication. For this, we can use the basic option:

curl -X POST --user user:passwd --basic https://httpbin.dev/basic-auth/user/passwd

The response of the above cURL request indicates that it's successfully authenticated:

{
  "authorized": true,
  "user": "user"
}

For further details on different mechanisms for handling authentication with cURL requests, refer to our dedicated guide.

Further cURL Options

Sending cURL POST requests often require additional configuration for the request. Here are additional cURL arguments and their usage:

Argument Full Option Description
-x --proxy Use proxy with cURL, either as HTTP or SOCKS
-Z --parallel Send multiple cURL requests in parallel
-L --location Make cURL follow request redirects
-O --output Save the response binary data to a local directory
-I --head Use the request HEAD HTTP method
-b --cookie Add request cookies
-v --verbose Sends the request in verbose mode, which include further debugging details
-k --insecure Skip SSL verification for HTTPs connection

For further details on sending requests with cURL, refer to our dedicated guide.

How to Use cURL For Web Scraping

In this article, we'll go over a step-by-step guide on sending and configuring HTTP requests with cURL. We'll also explore advanced usages of cURL for web scraping, such as scraping dynamic pages and avoiding getting blocked.

How to Use cURL For Web Scraping

Related Articles

Guide to using JSON with cURL

Learn how to send JSON with `cURL` using files, inline data, environment variables, and `jq`. Includes real-world examples for Slack & Google Translate.

CURL
Guide to using JSON with cURL

How to Ignore cURL SSL Errors

Learn to handle SSL errors in cURL, including using self-signed certificates. Explore common issues, safe practices.

CURL
SSL
How to Ignore cURL SSL Errors

How to Use cURL to Download Files

Master file downloads with curl and discover advanced use cases.

CURL
TOOLS
How to Use cURL to Download Files

cURL vs Wget: Key Differences Explained

curl and wget are both popular terminal tools but often used for different tasks - let's take a look at the differences.

CURL
HTTP
TOOLS
cURL vs Wget: Key Differences Explained

How to Use cURL GET Requests

Here's everything you need to know about cURL GET requests and some common pitfalls you should avoid.

CURL
How to Use cURL GET Requests

Sending HTTP Requests With Curlie: A better cURL

In this guide, we'll explore Curlie, a better cURL version. We'll start by defining what Curlie is and how it compares to cURL. We'll also go over a step-by-step guide on using and configuring Curlie to send HTTP requests.

CURL
HTTP
TOOLS
Sending HTTP Requests With Curlie: A better cURL