Logo

dev-resources.site

for different kinds of informations.

How to make API request from command line with CURL

Published at
4/9/2020
Categories
curl
https
api
request
Author
iggredible
Categories
4 categories in total
curl
open
https
open
api
open
request
open
Author
10 person written this
iggredible
open
How to make API request from command line with CURL

Why learn it?

cURL (Client URL) is a powerful command line tool for working with URLs.

If you want to test API outputs for your programs, you can use softwares like Advanced REST Client, POSTMAN, or Postwoman. But if you want to do something quick, universal (easily installed in most OS), and also as powerful, you can use cURL. It takes a little more learning, but it is worth it.

Fun fact: someone suggested to revise the abbreviation to use recursive acronym.



"Curl Url Request Library" (CURL)


Enter fullscreen mode Exit fullscreen mode

Which, when expanded, becomes:



Curl Url Request Library Url Request Library -> 
Curl Url Request Library Url Request Library Url Request Library ->
Curl Url Request Library Url Request Library Url Request Library Url Request Library ->
... etc


Enter fullscreen mode Exit fullscreen mode

cURL can be used with many protocols:



DICT, FILE, FTP, FTPS, GOPHER, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, TELNET, TFTP


Enter fullscreen mode Exit fullscreen mode

I will cover only one here, HTTPS, because I think that will be the most useful ones for testing APIs (another useful one to learn is FTP). This article is a basic introduction to cURL. If you wish to learn more, check out resources.

I will use jsonplaceholder site throughout this article. Let's get started!

Basic usage - cURL with GET

The basic syntax is:



curl your-url


Enter fullscreen mode Exit fullscreen mode

For example:



curl https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

cURL will return web page response.

cURL with query params

Sometimes we need to use query params to filter our search. There is no additional option that we need to pass. Recall that query string params have the following syntax:



?param1=value1&param2=value2&paramN=valueN:



Enter fullscreen mode Exit fullscreen mode

For example:



curl https://jsonplaceholder.typicode.com/posts?userId=2&id=19


Enter fullscreen mode Exit fullscreen mode

cURL with header information

If you want to see header information about your request, add -i
:



curl -i https://jsonplaceholder.typicode.com/posts?userId=2&id=19


Enter fullscreen mode Exit fullscreen mode

It will add something like:



HTTP/2 200
date: Tue, 07 Apr 2020 12:47:16 GMT
content-type: application/json; charset=utf-8
set-cookie: __cfduid=d995d97cc8c7b809dbd5a490b2db28e2f1586263636; expires=Thu, 07-May-20 12:47:16 GMT; path=/; domain=.typicode.com; 
...
server: cloudflare
cf-ray: 5803db316c66f19e-ATL

// Endpoint response here


Enter fullscreen mode Exit fullscreen mode

cURL + downloading file

You can download file with -O option. On today's google main page, I see this cool gif and I want to download it.

Google main page screenshot on April 8th 2020

Once you locate the URL with inspect, you can download it with:



curl -O https://www.google.com/logos/doodles/2020/thank-you-emergency-services-workers-6753651837108755-law.gif


Enter fullscreen mode Exit fullscreen mode

It downloads to your current directory where you run that command.

If you want to rename your file, you can use -o. The syntax is curl -o new-file-name file-location-url
. For example:



curl -o iggy-google-image.gif  https://www.google.com/logos/doodles/2020/thank-you-emergency-services-workers-6753651837108755-law.gif


Enter fullscreen mode Exit fullscreen mode

You don't have to save it to current location. You can change its directory and file, like:



curl -o ~/Images/iggy-google-image.gif https://www.google.com/logos/doodles/2020/thank-you-emergency-services-workers-6753651837108755-law.gif


Enter fullscreen mode Exit fullscreen mode

cURL with POST

By default cURL runs a GET request. To use different HTTPS method, you can use -X (--request). To do POST request with cURL, the syntax is:



curl -X POST my-url


Enter fullscreen mode Exit fullscreen mode

Btw, our GET cURL earlier:



curl https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

The code above is technically the same as -X GET, because if we don't explicitly specify, it will default to GET:



curl -X GET https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

Sending data (-d)

We probably can't do much with POST if we just do curl -X POST my-url. We need to pass it some data in our request body. To pass data in cURL, use -d. For example:



curl -X POST -d 'title=test title&body=no body' https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

If you notice, the format of our body is similar to query params: key=value&key2=value2&...

To pass JSON type data in body:



curl -X POST -d '{"title": "hello json"}' https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

Adding header

When we are sending data, we may need to include header. For example, to notify the server that we are sending JSON, we add "Content-type: application/json". We can use -H.

This is what our request looks like with header added:



curl -X POST -d '{"title": "hello json"}' -H "Content-type: application/json" https://jsonplaceholder.typicode.com/posts


Enter fullscreen mode Exit fullscreen mode

You can add multiple headers with cURL by having multiple -H in one request:



curl -X POST -d '{"title": "hello json"}' -H "Content-type: application/json" -H "Accept-Charset: utf-8" -H "Accept-Language: en-US" https://jsonplaceholder.typicode.com/posts

Enter fullscreen mode Exit fullscreen mode




cURL with PUT

Just like POST, we can do PUT request:



curl -X PUT -d '{"title": "hello put"}' https://jsonplaceholder.typicode.com/posts/1

Enter fullscreen mode Exit fullscreen mode




cURL with PATCH

Curl also works with PATCH:



curl -X PATCH -d '{"title": "hello patch"}' https://jsonplaceholder.typicode.com/posts/1

Enter fullscreen mode Exit fullscreen mode




cURL with DELETE

Curl works with DELETE:



curl -X DELETE https://jsonplaceholder.typicode.com/posts/1

Enter fullscreen mode Exit fullscreen mode




Conclusion

This is a good place to stop. This article provides a guide how to get started with cURL. There are many more features I did not cover. I would suggest checking out the resources below to learn more.

Thanks for reading. Happy coding!

Resources

request Article's
30 articles in total
Favicon
important status code to be known as API developer
Favicon
HTTP Headers in API
Favicon
Mastering API Request Chaining: Essential Techniques for Developers
Favicon
Increasing API Reliability: Adding Timeouts to Node.js Fetch
Favicon
Python and APIs: how to use Python to connect and interact with APIs
Favicon
Rails Request specs with arbitrary JSON params
Favicon
Criando react hook personalizado para fazer requisições
Favicon
HTTPS: Is it better than HTTP?
Favicon
Python: pytest accessing Flask session and request context variables.
Favicon
[HELP] Spring Boot: field is not set in request
Favicon
I'm Looking for Beta Readers
Favicon
An alternative API mocking library for frontend developers
Favicon
How to send request to join a organization in github
Favicon
Negotiating Languages with Ruby: A Journey through Linguistic Diversity
Favicon
Event-driven architecture over standard client-server aproach
Favicon
Roll your own Request object in Laravel
Favicon
Mezon wrapper for processing HTTP requests
Favicon
Preventing memory leak by handling errors and request cancellations separately in Axios
Favicon
How do you present a PR?
Favicon
How can i access the request object inside a nuxtJS module?
Favicon
React Js Axios Post Request Example Tutorial
Favicon
How to make API request from command line with CURL
Favicon
Request for Node.js has been deprecated
Favicon
NodeJS http homepage 20: request url with link menu
Favicon
Sharing the context with the model in an Express app
Favicon
A simple and useful #react component for helping with asynchronous loading/fetch data
Favicon
Feature request: @mention a user
Favicon
We want Dev.to REST API :)
Favicon
Perform small coding tasks for a reliable source of income?
Favicon
When to request data in SPA

Featured ones: