Pre-requisites: Knowledge about basic networking protocols, REST API fundamentals.
As a backend developer, you mostly interact with requests and responses. When you are doing this on your local machine you might use a tool like Postman, which comes with a nice GUI that allows you to send requests and responses. But what if you are doing all that on a server, which only has a Linux BASH, without any GUI? In that case, a command-line tool like CURL is what we will use.
If you are on a macOS or Linux machine then you already have CURL. For all the windows folks out there, I would recommend downloading Git-bash which has CURL and is also a better command line in general.
First of all to check whether you have a proper setup just open the command line and type curl --help
, and you should see something like this.
The output may differ a little bit depending on the version and OS, but it should not tell you that CURL is not recognized.
Internet requests deal with sending data from one computer to another, primarily between a client and a server. But we are not going to set up our own server. For now we will use a website called JSON placeholder which is just a fake online REST API that you can use.
If you go to this website you will see that they have several routes, for example.
Clicking on any of them will send you some fake data, which mimics a GET request.
A GET request is one in which you retrieve data using REST API.
Let’s start. Copy the link to any of these; I am going to copy the link for posts, which is a GET request. Give the following command in your CLI.
curl https://jsonplaceholder.typicode.com/posts
You will see the following response, which has over 100 posts data and all the info about how much time the request took.
If you want data for only one post you can add /id at the end of the URL like this.
curl https://jsonplaceholder.typicode.com/posts/3
If you want to see more info about the request like the headers you can use -i flag.
curl -i https://jsonplaceholder.typicode.com/posts/3
It will show you the header info,
like the content-type and cache-control’s maximum age and a lot more.
You can store all the responses in a file on your system. To do that, first navigate to that folder in your command line and create a file where you want the data to be stored. I have a file like this where I store the data.
You can replace the lowercase o in the command with uppercase O and remove the file name. It will download the data on your system and create a file on its own.
Using a POST request you can save data to the server via REST API. Note that the fake data client we are using will not save any actual data on the server but will mimic that process. In order to send the POST request we add the data using -d flag and specify the type of request using -X flag. For example, to send the post request we use the following syntax.
curl -d “requestBody” -X POST url
The client we are using has the POST request at https://jsonplaceholder.typicode.com/posts URL.
Use the following command:
1
curl--data "title=Hello&body=Hello world" - X POST https: //jsonplaceholder.typicode.com/posts
It will send the title for the new post as Hello and body as Hello world. On sending this we get the following response.
Now, send a PUT request which is used to update the data. Use the following syntax.
curl -d "title=Hello" -X PUT https://jsonplaceholder.typicode.com/posts/3
It will set the title of the post with id=3 to Hello. Here’s the response.
And to send a DELETE request you can remove the -d and the data inside curly braces and put -X DELETE like this.
curl -X DELETE https://jsonplaceholder.typicode.com/posts/3
Sometimes you might be working with authenticated routes and you might want to add headers. To do that use the following command.
curl --header "headerName: headerValue" URL
In order to upload files, we use the -f option which adds enctype = ”multipart/form-data” to the request, which tells the receiver that it is a file. Setting up a server to receive these files is way beyond the scope of this article, but you can send the file in the following format.
$ curl -F ‘data=@path/to/local/file’ UPLOAD_ADDRESS
WGet stands out majorly in its ability to download recursively, or to download anything that is referred from a remote resource. Whereas CURL is kind of a single shot, it transfers data just from the URL that the user specifies with no recursive downloading of the pages that are embedded in the specified URLs. The difference between CURL and wGet is best explained by the creator of the CURL tool here.