Proxy locations

Europe

North America

South America

Asia

Africa

Oceania

See all locations

Network statusCareers

Back to blog

How to Send POST Requests With cURL

How to Send POST Requests With cURL
Yelyzaveta Nechytailo

Yelyzaveta Nechytailo

2023-06-093 min read
Share

In this tutorial, you’re going to learn how to send POST requests with cURL. cURL is a powerful command-line tool for transferring data over various network protocols, including HTTP, HTTPS, FTP, and more. Since POST is a request method of HTTP & HTTPS protocols, cURL makes sending POST requests a one-line command that you can run in your terminal easily.  

Tutorial: Sending POST requests

First, install cURL if you haven’t installed it already. You can find the installation instructions in our How to Use cURL With Proxy blog post. Next, take a look at the below table:

Flag Long Option Argument What it does
-X --request POST Specifies HTTP method
-H --header User Agent: Chrome Specifies Header content
-F --form file=@/path/file.jpg Attach form data or files.
-u --user username:password Set credentials
-d --data data Sets the request body data
-v --verbose N/A Display detailed Information about Request & Response

This table represents all the necessary command-line options of cURL that you’ll be using in the next few sections. You don’t need to memorize all of them right away, just briefly go over them. Once you start to use cURL daily, they’ll become part of your muscle memory. And whenever you need, you can always access all the available cURL options including the above using the --help option:

curl --help

The basic syntax for sending a POST request using cURL is as below:

curl -X POST -d "Hello" https://example.com/api

Notice the -X flag followed by POST, it tells cURL to make a request using the HTTP protocol’s POST method, and the -d flag sets request data as Hello and sends it to the website https://example.com/api. The -X flag is the short form of the command line option --request. Check out the above table for learning all the long forms of the various command line options.

Specifying the Content-Type

Like any HTTP request, POST requests created using cURL can also have custom headers. For specifying the Content-Type header, you’ll have to set it using the header flag. 

curl -X POST -H "Content-Type: text/plain" -d "Hello" https://example.com/api

In the above command, there’s an additional -H flag which lets users send custom HTTP request headers via cURL. In this case, by specifying the Content-Type header as text/plain you’re letting the web server know that the request body data is in TEXT format. 

Posting JSON

It’s also possible to send JSON data in the request body. All you need to do is set the appropriate Content-Type header and pass the JSON data with the -d flag. cURL will make a POST request with the JSON data specified in the argument.

curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' https://example.com/api

Posting XML

Similar to JSON, you can also send XML in the request body. You’ll have to make changes to the request header and set it to application/xml.

curl -X POST -H "Content-Type: application/xml" -d '<?xml version="1.0" encoding="UTF-8"?><root><name>John Doe</name><age>30</age></root>' https://example.com/api

Sending a file/multiple files via POST

To send a file via cURL POST, you’ll have to use the -F flag. Pay attention to the capitalization of the letter “F”. All of the cURL flags or command line options are case-sensitive.

curl -X POST -F "file=@/path/to/img.png" https://example.com/api/upload

As you can see, the above command is uploading an image file. Right after the -F the file path of the image was given. You can also use multiple -F flags to send multiple files to the server as below:

curl -X POST -F "file=@/path/to/img1.png" -F "file=@/path/to/img2.png" https://example.com/api/upload

Sending authentication credentials

You can use the -u flag or the --user option to specify the username & password for basic authentication. cURL will automatically create the Authorization header based on your input. 

curl -u username:password https://example.com/login

You’ll have to replace username and password with the actual authentication credentials. Also, don’t forget to replace the example URL with your own.

Conclusion

cURL is a lightweight yet powerful tool for sending POST requests from CLI. With just a single line of command, you can easily transmit data in various formats such as JSON, XML, or file uploads. cURL's simplicity and flexibility make it a popular choice for developers. While it may not offer a graphical interface, cURL provides a versatile and efficient way to interact with servers. Whether you're a seasoned developer or a complete beginner, mastering cURL is a valuable skill that’ll help you in various stages of your career. If you're new to cURL, we recommend reading our article on how to send GET requests with cURL.

You can also check out the How to Use cURL With REST API and How to Use cURL With Python articles available on our blog. As usual, if you have any questions, please contact us at hello@oxylabs.io or via the live chat – our professional team is always ready to provide you with the needed assistance. 

Frequently asked questions

What is a cURL POST?

A cURL POST refers to a POST request made using cURL. cURL is a versatile and widely used CLI that allows you to make HTTP requests. When making a POST request with cURL, you're sending data to a server or an API. The data is typically included in the body of the request. By using cURL's -X POST option and providing the necessary data and URL, you can initiate a POST request.

Can I use cURL instead of Postman?

Absolutely. cURL has all the necessary features to make HTTP requests. If you're familiar with the Command line interface and prefer working with command line tools, then cURL’s lightweight CLI will be immensely helpful. However, cURL doesn’t have a GUI like Postman. Also, some of the advanced features of Postman such as the history of requests, test automation, team collaboration, etc. are not available in cURL. 

What are the alternatives to cURL POST?

Both Postman & Insomnia are popular among developers as an alternative to cURL. These two libraries can also make POST requests. You can also use Python or other Scripting Languages such as Ruby, Go, etc. All these Programming languages have HTTP libraries to send GET & POST requests.

What is the difference between POST and GET cURL?

GET and POST both serve distinct purposes in cURL. They differ primarily in how data is transmitted to the server or API. The GET method relies on URL parameters and queries to send data and retrieve information. On the other hand, the POST method sends data through the request body, which hides data from the URL. As a result, the POST method is considered more secure than GET, making it a preferred choice for transmitting sensitive information.

About the author

Yelyzaveta Nechytailo

Yelyzaveta Nechytailo

Senior Content Manager

Yelyzaveta Nechytailo is a Senior Content Manager at Oxylabs. After working as a writer in fashion, e-commerce, and media, she decided to switch her career path and immerse in the fascinating world of tech. And believe it or not, she absolutely loves it! On weekends, you’ll probably find Yelyzaveta enjoying a cup of matcha at a cozy coffee shop, scrolling through social media, or binge-watching investigative TV series.

All information on Oxylabs Blog is provided on an "as is" basis and for informational purposes only. We make no representation and disclaim all liability with respect to your use of any information contained on Oxylabs Blog or any third-party websites that may be linked therein. Before engaging in scraping activities of any kind you should consult your legal advisors and carefully read the particular website's terms of service or receive a scraping license.

Related articles

Get the latest news from data gathering world

I’m interested

Scale up your business with Oxylabs®