In this article, we will discuss the cURL tool in Linux. The cURL tool is used for transferring the data from or to a server. It supports many protocols, and http is one of them. cURL is used to transfer the data from URL. It has so many tricks to offer, such as http post, ftp post, user authentication, proxy support, file transfer, SSL connections, cookies, and similar.
Prerequisites
Besides having a Terminal open, you need to ensure that you have curl installed on your system.
How to do it
We will learn about HTTP GET and POST methods using curl in Linux. Now, we will see examples of GET and POST:
GET: The HTTP GET method is used to request a data from a specified resource. The command is the example of HTTP GET and is accepting JSON formatted data:
$ curl -H "Content-Type:application/json" -H "Accept:application/json" -X GET http://host/resource/name
Another example of HTTP GET, which is accepting XML formatted data is as follows:
$ curl -H "Content-Type:application/xml" -H "Accept:application/xml" -X GET http://host/resource/name
POST: The HTTP POST method is used to send the data to server. It is used to create as well as update a resource.
A simple POST example is as shown:
$ curl --data "name1=value1&name2=value2" http://host/resource/name
Run the following curl command to upload a file:
$ curl -T "{File,names,separated,by,comma}" http://host/resource/name
How it works
Now we will learn about the options used in the curl command:
- First, we learned about the
GETmethod in thecurlcommand. We accepted two types of data: JSON and XML. We used the-Xoption and specified the request.-Hspecifies the header. - Second, we learned about the
POSTmethod in thecurlcommand. The--dataflag is used for thePOSTrequest. In the simple example, we are just posting the simple data and in the next, we are uploading files using the-Toption.
