How To Send cURL POST Requests?

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.

curl web scraping article banner

Provided by Scrapfly

This knowledgebase is provided by Scrapfly data APIs, check us out! 👇