How to Use cURL Post Data from File?

cURL is a command/tool that transfers data over a network. cURL is often used to download files from a server, upload files to a server, send HTTP requests, and more.

You can use cURL to send GET, POST, PUT, DELETE, and other HTTP requests, and it will display the response from the server in the terminal.

This guide aims to illustrate the usage of the cURL command regarding post data from the file. The content that demonstrates the guideline is mentioned-below:

Let’s start the article with the installation of the cURL command.

Prerequisite: Install cURL Command

A cURL command is a beneficial tool for debugging and testing web applications. To install the “cURL” command in the system, follow the below script:

$ sudo apt install curl    # Ubuntu
$ yum install curl         # RHEL / CentOS / Fedora

After the installation is completed. Let’s explore the “cURL” command to send Post data.

How to Use cURL to Post Data From File?

To use the cURL command for sending POST data from a file, the “-d” option is utilized with an @ symbol. It requires the path to the file or “URL” that contains the POST data:

Syntax

$ curl -d @filename [URL]

Example 1: Using cURL to Send POST Data From a File

To send the post data from the file, the “curl” command is utilized by specifying the name of the file. To do so, the “file.txt” is carried out, which is located in the home directory:

$ curl -X POST -d @file.txt https://itslinuxfoss.com/privacy-policy/

The above script will send a POST request to the specified URL along with the data in the “file.txt” file as the request body.

Example 2: Specify the Content-Type

To specify the type of content of the POST data using the “-H” option. The “-X” option is utilized for the HTTP request as “POST”. Finally, the “data.json” file is carried out to send the request to the URL “https://itslinuxfoss.com/privacy-policy/”:

$ curl -X POST -H "Content-Type: application/json" -d @data.json https://itslinuxfoss.com/privacy-policy/

The output confirms that POST request has been sent with a content type of application/json.

Example 3: Specify the POST Data as a Binary File

Users can use the “–data-binary” option to specify the POST data as a binary file. For this, the “curl” command is used to send the request along with the binary file “binary.dat”:

$ curl -X POST --data-binary @binary.dat https://itslinuxfoss.com/privacy-policy/

This will send a POST request with the contents of the file “binary.dat” as the request body.

Example 4: Specify the POST Data Directly

Users can specify the POST data directly on the command line using the “-d” option rather than a file. To do so, follow the below pattern:

$ curl -X POST -d "name=value" https://itslinuxfoss.com/privacy-policy/

This will send a POST request with the specified data as the request body.

That is all from using the cURL command to Post Data from the file.

Conclusion

The “cURL” command sends the POST data from a file with the “-d” option to a specific URL. This command requires the path to the file containing the local machine’s POST data. Additionally, users can specify the content type and binary files or send POST data directly. This guide has provided various ways to Post data from the file using the “cURL” command.