DEV Community

Cover image for How to Easily use cURL for HTTP Requests
Aditya Sridhar
Aditya Sridhar

Posted on • Originally published at adityasridhar.com

How to Easily use cURL for HTTP Requests

Originally published in adityasridhar.com

Every developer needs to know a bunch of tools to be effective. cURL in one such tool 😄

In this article I will explain how curl can be used to make HTTP requests.

What is cURL

cURL is basically used to transfer data using Internet Protocols for the given URL.

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.

In this article we will deal only with making HTTP requests from Curl.

Pre-requisite

You can check if you have Curl Installed in your System using the command.

curl --version
Enter fullscreen mode Exit fullscreen mode

If Curl is not there in your system you can install it from this URL https://curl.haxx.se/dlwiz/.

How to Use Curl for HTTP Requests

I have built 2 REST API Endpoints using NodeJS. One endpoint supports GET request and the other endpoint supports POST request.

In this article, we will be calling the GET and POST endpoints using Curl.

Please clone the NodeJS code into your local from this github repo

The repo has instructions on how to clone and run the NodeJS code.

After cloning the code, go into the project folder and start the application using the following command.

node server.js
Enter fullscreen mode Exit fullscreen mode

The application runs on localhost port 3000.

GET Request with cURL

The application has a GET endpoint /sample. This endpoint accepts a query parameter called name.

Let's call this API endpoint using curl.

Go to a new command prompt and type the following command

curl http://localhost:3000/sample?name=aditya
Enter fullscreen mode Exit fullscreen mode

This will give the following output

{"text":"Hello aditya"}
Enter fullscreen mode Exit fullscreen mode

You can also get a verbose result using Curl. Run the following command

curl -v http://localhost:3000/sample?name=aditya
Enter fullscreen mode Exit fullscreen mode

-v is used to get verbose output.

This will give the following output.

*   Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /sample?name=aditya HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 15 Nov 2018 12:36:21 GMT
< Connection: keep-alive
< Content-Length: 23
<
{"text":"Hello aditya"}* Connection #0 to host localhost left intact
Enter fullscreen mode Exit fullscreen mode

The verbose result has details like status code, Content Type, Content Length and so on. It can be used to get a better idea of what happened during the HTTP request.

The status code is 200 which indicates the HTTP request was successful.

The Content-type of the response is JSON.

The Content-Length indicates the size of the response. Here the response size is 23 bytes.

POST request with cURL

The application has a POST endpoint /test. This endpoint accepts a post body of the following format.

{
    "value":"nodejs"
}
Enter fullscreen mode Exit fullscreen mode

In order to make the POST call, type the following command.

curl --header "Content-Type: application/json" -d "{\"value\":\"node JS\"}" http://localhost:3000/test
Enter fullscreen mode Exit fullscreen mode

--header indicates the content type of the post body. Here it is JSON.

-d is used to send the post body content.

The output of this command is shown below.

{"text":"Post Request Value is  node JS"}
Enter fullscreen mode Exit fullscreen mode

To get Verbose result use the following command

curl -v --header "Content-Type: application/json" -d "{\"value\":\"node JS\"}" http://localhost:3000/test
Enter fullscreen mode Exit fullscreen mode

The output is shown below.

*   Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /test HTTP/1.1
> Host: localhost:3000
> User-Agent: curl/7.55.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 19
>
* upload completely sent off: 19 out of 19 bytes
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 15 Nov 2018 13:03:37 GMT
< Connection: keep-alive
< Content-Length: 41
Enter fullscreen mode Exit fullscreen mode

Additional Options provided by command line

The existing headers can be modified using -H option.

Run the following Command to modify the User-Agent header to Dummy Agent

curl -v -H "User-Agent:Dummy Agent" http://localhost:3000/sample?name=adi
Enter fullscreen mode Exit fullscreen mode

The output for the above command is shown below

*   Trying ::1...
* TCP_NODELAY set
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /sample?name=adi HTTP/1.1
> Host: localhost:3000
> Accept: */*
> User-Agent:Dummy Agent
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 15 Nov 2018 13:33:27 GMT
< Connection: keep-alive
< Content-Length: 20
<
{"text":"Hello adi"}* Connection #0 to host localhost left intact
Enter fullscreen mode Exit fullscreen mode

In the above output it can be seen that User-Agent has become Dummy Agent.

Now Let's say you want to remove the Host Header. This can be done by running the following command.

curl -v -H "Host:" http://localhost:3000/sample?name=adi
Enter fullscreen mode Exit fullscreen mode

The output for the above command is given below and it can be seen that Host is not there anymore

*   Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 3000 failed: Connection refused
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 3000 (#0)
> GET /sample?name=adi HTTP/1.1
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json
< Date: Thu, 15 Nov 2018 13:38:37 GMT
< Connection: keep-alive
< Content-Length: 20
<
{"text":"Hello adi"}* Connection #0 to host localhost left intact
Enter fullscreen mode Exit fullscreen mode

-H can be used to add new headers as well. This can be done using the syntax curl -v -H "newheader: headervalue"

References

cURL's Documenation: https://ec.haxx.se/

More Info on Using Curl for HTTP requests https://ec.haxx.se/http.html

Congrats 😄

You now know how to use cURL for basic HTTP requests. This article covers a very small portion of what curl can actually do.

To know more about curl you can check the documenation links I have provided above.

Feel free to connect with me in LinkedIn or follow me in twitter.

If you liked this post, you can checkout my website https://adityasridhar.com for other similar posts

Top comments (6)

Collapse
 
pbnj profile image
Peter Benjamin (they/them) • Edited

Nice introduction to cURL!
Tiny nitpick: you can wrap your payload (i.e. -d) in single quotes to avoid having to escape inner double quotes.
Example:

$ curl -H “Content-Type: application/json“ -d{ “value”: “node.js” }’ https://localhost:3000/test
Collapse
 
adityasridhar profile image
Aditya Sridhar

Thank you :)

Actually I tried single quotes initially. Works well in mac, but in windows it threw an error. So switched it back to double quotes :D

Collapse
 
pbnj profile image
Peter Benjamin (they/them)

Huh, didn't know that.
Thanks for the info.

Collapse
 
chenge profile image
chenge

How about postman app?

Collapse
 
david_j_eddy profile image
David J Eddy

IMO, always learn and understand a tool via the CLI first. Then pick a GUI of your choice.

"CLI, everything else is abstraction" - DJE

Collapse
 
adityasridhar profile image
Aditya Sridhar • Edited

POSTMAN is great too. I use that a lot. But I feel knowing curl always helps :D