shell script|May 09, 2018|2 min read

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl - Your friend for Rest APIs/Calls - Basic Commands

Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally open any web URL through it, and save files on public URLs through it. Its pretty light weight.

All you need is a terminal or console.

A Simple GET Rest Call

``` curl https://yourhost/api/getthings

OR

curl -X GEThttps://yourhost/api/getthings


And, you will get the result of this rest endpoint on the console or terminal.

Note: In above example, I assume there is no authentication asked by REST endpoint.

<h2>A POST Call</h2>
Lets assume you have a REST endpoint which takes some data in a POST call.

curl -X POST -H “Content-Type: application/json” -d ’{“msg”: {“to”: “abc-at-smail”}}’ localhost:3000/api/queue/v1


Here, I'm passing a json data to rest endpoint, and mentioned that its a POST call by -X switch.

Note: You need to mention header "Content-Type: application/json" to pass a json to your endpoint. Else, your end point will take it as a string not json.

<h2>Pass Http Headers</h2>
You can pass as many headers to your rest endpoint as you want. Curl provides -H switch, and you pass multiple headers.

curl -H “Host: www.host.com” -H “Authorization: xyz” -H “Content-Type: application/json” localhost:3000/api/queue/v1


<h2>Download static resource like Image</h2>
Assume you have a public url for image like, and you want to download to your machine or server.

curl https://www.owasp.org/images/1/1d/Session-Management-Diagram_Cheat-Sheet.png -o file.png


You specify -o flag to write to a file and give a name. Else, this will display content of that resource on console.

<h2>Download html</h2>
Similarly, you can download html of resource.

curl https://www.owasp.org/index.php/Session_Management_Cheat_Sheet -o file.html


This will download the htmlonly, not any other resources like image or css or javascript.

## Using curl for upload file on AWS S3
See [Uploading to S3 witj Curl](/tutorials/how-upload-aws-s3-curl/)

If you have any difficulty using curl, put it in comment below.

Enjoy

Related Posts

Python - Dont Use Static or Class variables with Self

Python - Dont Use Static or Class variables with Self

While doing code review of one of junior, I encountered following code: What…

Moment.js - How to perform date relatedd arithmetic in javascript/NodeJs

Moment.js - How to perform date relatedd arithmetic in javascript/NodeJs

Introduction In your backend and frontend projects, you always need to deal with…

How to install Mongo DB Driver for Php 7.x

How to install Mongo DB Driver for Php 7.x

The simplest way to install driver for php is using pecl. When I tried to run…

ReactJS - How to create ReactJS components

ReactJS - How to create ReactJS components

Introduction In this post, we will talk about basic of how to create components…

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

How to Fix Drupal Mysql error - Communication link failure: 1153 Got a packet bigger than 'max_allowed_packet' bytes

Introduction While this topic may applicable to all mysql/mariadb users who…

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Python SMTP Email Code - How to Send HTML Email from Python Code with Authentication at SMTP Server

Introduction This post has the complete code to send email through smtp server…

Latest Posts

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…