How to Send POST Requests With cURL

Flipnode on Jun 09 2023

blog-image

In this tutorial, we will explore the process of sending POST requests using cURL. cURL is a versatile command-line tool designed for transferring data across different network protocols, such as HTTP, HTTPS, and FTP. With its simple syntax, cURL allows you to send POST requests effortlessly, condensing the process into a concise one-line command that can be executed directly from your terminal.

Tutorial: Sending POST requests

To begin, if you haven't installed cURL yet, please refer to our How to Use cURL With Proxy blog post for detailed installation instructions. Once you have cURL installed, let's familiarize ourselves with the command-line options that will be used in the upcoming sections. The overview of these options:

  • -X: --request, POST, Specifies the HTTP method
  • -H: --header, User Agent: Chrome, Specifies the content of the header
  • -F: --form, file=@/path/file.jpg, Attaches form data or files
  • -u: --user, username:password, Sets credentials
  • -d: --data, data, Sets the request body data
  • -v: --verbose, N/A, Displays detailed information about the request and response

While you don't need to memorize all of these options immediately, take a moment to familiarize yourself with them. With regular usage of cURL, they will become second nature. If you ever need to access all the available cURL options, including the ones mentioned above, you can use the --help option:

curl --help

Now, let's explore the basic syntax for sending a POST request using cURL:

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

In this example, the -X flag followed by POST instructs cURL to make a request using the POST method of the HTTP protocol. The -d flag sets the request data as "Hello" and sends it to the URL https://example.com/api. Note that -X is the short form of the --request command-line option. For a complete list of long forms of various command-line options, refer to the table provided above.

Specifying the Content-Type

When making POST requests using cURL, you can include custom headers, just like any other HTTP request. To specify the Content-Type header, you need to use the -H flag.

Here's an example command that sets the Content-Type header to "text/plain" while sending a POST request:

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

In this command, the -H flag allows you to set the request headers. By specifying "Content-Type: text/plain", you inform the web server that the request body data is in plain text format.

Posting JSON

If you want to send JSON data in the request body using cURL, you can do so by setting the Content-Type header appropriately and using the -d flag to pass the JSON data.

Here's an example command that makes a POST request with JSON data:

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

In this command, the -H flag is used to set the Content-Type header as "application/json", indicating that the request body data is in JSON format. The -d flag is used to specify the JSON data as '{"key":"value"}'. cURL will send the POST request with the provided JSON data to the specified URL.

Posting XML

If you want to send XML data in the request body using cURL, you can do so by modifying the request header and setting it to "application/xml".

Here's an example command that makes a POST request with XML data:

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

In this command, the -H flag is used to set the Content-Type header as "application/xml", indicating that the request body data is in XML format. The -d flag is used to specify the XML data enclosed in single quotes. cURL will send the POST request with the provided XML data to the specified URL.

Sending a file/multiple files via POST

To send a file via cURL using the POST method, you need to use the -F flag (note the capitalization). Please remember that cURL flags and command line options are case-sensitive.

Here's an example command that uploads an image file:

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

In the above command, the -F flag is used to specify that a file is being uploaded. After the -F flag, you provide the file path preceded by "file=@" to indicate the file to be uploaded. In this case, the image file located at /path/to/img.png is being sent to the server.

If you want to upload multiple files, you can use multiple -F flags as shown in the following command:

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

This command uploads both img1.png and img2.png files to the specified server endpoint by using separate -F flags for each file.

Sending authentication credentials

To provide basic authentication credentials with cURL, you can utilize the -u flag or the --user option. This allows you to specify the username and password, and cURL will generate the appropriate Authorization header for authentication.

Here's an example command:

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

In the above command, replace "username" and "password" with your actual authentication credentials. Additionally, make sure to update the URL to your own target endpoint. cURL will include the generated Authorization header in the request to authenticate your credentials.

Conclusion

cURL is a command-line tool that offers a lightweight and robust solution for sending POST requests. With its concise syntax, you can effortlessly send data in different formats like JSON, XML, or even upload files. Despite its lack of a graphical interface, cURL remains a favored option among developers due to its versatility and effectiveness. Whether you're an experienced programmer or just starting out, mastering cURL is a valuable skill that will benefit you throughout your professional journey. It empowers you to efficiently interact with servers and enhances your capabilities as a developer.

News and updates

Stay up-to-date with the latest web scraping guides and news by subscribing to our newsletter.

Subscribe

Related articles

thumbnail
ProxiesHow to Use Chrome Browser Proxy Settings

Learn how to configure and utilize Chrome browser proxy settings effectively for enhanced privacy and optimized web browsing.

Flipnode
author avatar
Flipnode
8 min read
thumbnail
ProxiesProxies for Cybersecurity Solutions

Enhance your cybersecurity with reliable proxy solutions. Discover how proxies can bolster your online defenses. Expert insights inside. Enhance your cybersecurity with reliable proxy solutions. Discover how proxies can bolster your online defenses. Expert insights inside.

Flipnode
author avatar
Flipnode
11 min read
thumbnail
ScrapersWeb Scraping With RegEx

Regular Expressions (RegEx) are powerful pattern matching tools that allow you to filter and extract specific combinations of data, providing the desired output.

Flipnode
author avatar
Flipnode
5 min read