cURL is a free and open-source command-line utility used for transferring data to or from a remote host with minimal user interaction. cURL works with primary protocols like HTTP, FTP, SCP, and SFTP.
- Curl Content-type Application/json
- Curl Set Content Type Header
- Curl Content Type X-www-form-urlencoded
- Curl Send Body
It allows users to upload and download data either using single commands or bash scripts. It also provides features such as user-authentication, proxy tunneling, download resume, form-based uploads, SSL certificates, and so much more. It is safe to say that cURL is more than an HTTP client.
This tutorial will walk you through one cURL functionality that allows users to perform HTTP post requests using file data.
- Curlformadd - add a section to a multipart/formdata HTTP POST SYNOPSIS. #include curl/curl.h. If the part is made for file upload, there are also a stored CONTENT-TYPE and a FILENAME. Below, we'll discuss what options you use to set these properties in the parts you want to add to your post.
- The Basic authentication used in HTTP (which is the type curl uses by default) is plain text based, which means it sends username and password only slightly obfuscated, but still fully readable by anyone that sniffs on the network between you and the remote server. To tell curl to use a user and password for authentication.
Curl is a Client side program. In the name cURL, c stands for Client and URL indicates curl works with URL’s. The curl project has a curl command line and also a libcurl library. In this article we will be focussing on the curl command line. Curl deals with a bunch of Internet Protocols like HTTP, FTP, SMTP, TELNET and so on. This is one of the often forgotten little gems in the curl arsenal of command line options.write-out or just -w for short, writes out information after a transfer has completed and it has a large range of variables that you can include in the output, variables that have been set with values and information from the transfer.
Before we get started, let me mention that this is not a beginner’s guide to cURL; you need a degree of prior know-how, especially knowledge of network protocols, HTTP requests, and more.
Before we can dive into using cURL to carry out POST requests, let us first get setup.
How to Install cURL
In most cases, you will find cURL installed in major Linux distributions by default. To confirm you have cURL, use the command
If you get an error:
Curl Content-type Application/json
You will need to install it before proceeding.
Use the default package manager to complete the installation as shown in the commands below:
Before discussing how to POST data from a file with cURL, allow me to talk about the POST request in brief. As usual, if you are already familiar with this, you can feel free to skip ahead; otherwise, stick around.
cURL POST Request
The HTTP post request is one of the most popular HTTP/HTTPS request methods used to send data to a remote host to create or update a resource.
Now:
Please do not confuse the method with PUT; although they’re quite similar, they have their differences.
The data sent using a POST request is mainly stored in the request body of the HTTP request.
For example, consider the below POST request that sends username and password as well as their values.
To send the above POST request using cURL, we can specify the commands as:
In the above command, we used the -d option to tell cURL to include the default headers, which is Content-Type: application/x-www-form-urlencoded
The -X option specifies the HTTP request method to use, in this case, HTTP POST request.
cURL Specify Content-Type
In some cases, we might want to specify [explicitly] the Content-Type when sending the request. The Content-Type entity in the header specifies the type of media of the resource we are sending. The media type is also commonly known as the MIME Type.
If you wish to learn about MIME Types, consider the resource provided below:
To specify the Content-Type in a cURL request, we can use the -H flag. For example, let us send MIME Type of application/JSON.
The above specifies that we want to send a JSON object to the provided URL. You can use this to read the values from a JSON file or send it as raw.
How to Read Data from a File
Suppose you want to make requests in the command-line using cURL, but you have the data to send stored in a file. You can use the Content-Type to specify the media type and then pass the file path containing the data. For this example, I will illustrate using a JSON object.
The JSON file (data.json) contains the following values:
To send this data from a JSON file, we can use the -d and then pass the filename as shown in the command below:
You can also use the –data-binary option.
You can also use a text file to send the data; all you need to specify is the Content-Type as text/plain.
Curl Set Content Type Header
For example, a text file (data.txt) containing the following values
Using the cURL command, simply send the request as:
You can use other file types you can use such as XML, HTML, and many more.
Curl Content Type X-www-form-urlencoded
Conclusion
In this tutorial, we have discussed how to make POST requests and pass data in various formats. It is good to keep in mind that besides being very powerful, cURL is also very flexible. It can combine a collection of options in a single command to create powerful requests. For more information, I recommend the cURL documentation as it is well written.
Curl Send Body
Thank you for reading, and have fun with cURL.