REST API using curl

REST API using curl

REST API using curl

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

You can interact with a REST API endpoint from Linux command line using curl command and today we will discuss how to make different REST API requests like HEAD, GET, POST, PUT, PATCH, and DELETE using Linux curl command.

If you wonder what REST API is, you can refer this blog post where I have given all the details to clear your doubts.

REST API using curl

Prerequisite

One Linux system with docker and jq installed

Setup:

We will use a docker container that has REST API application pre-configured for this demo. Login to your Linux shell and execute below command to launch a new docker container.

REST API HTTP HEAD request using Linux curl:

The HTTP HEAD method requests the headers that would be returned if the HEAD request’s URL was instead requested with the HTTP GET method. For example, if a URL might produce a large download, a HEAD request could read its Content-Length header to check the filesize without actually downloading the file.

REST API HTTP GET request using Linux curl:

GET method is used to retrieve resource information or to get information from a REST API endpoint. If the request is success, the endpoint response with a status code of 200 along with the requested data and if failed, response will contain the respective status codes like 404 or 400 etc.

Print REST API HTTP GET request details using Linux curl

If you observe the previous two command, we have added –silent option with curl command to get only data from the response using curl command. But if you want to see the entire response details of HTTP GET request, you can include –include and –verbose options with curl command.

REST API HTTP POST request with data from a file using Linux curl:

POST method is used to create a new resource. If the resource is created successfully the response will contain status code 201 and if failed, response will contain the respective status codes like 404 or 409 etc.

REST API HTTP POST request with data from command line using Linux curl:

If you observe the last command, we have passed the employee data from a file in the GET request using –data “<some_path/some_file>”, but you can also directly pass the data with the curl command itself as shown below.

How to use environment variable in data option of Linux curl command?

So far, we have learned how to POST with data from file and from command line using Linux curl command. But sometimes you may want to pass the data dynamically using Linux environment variable in your curl command.

Next, we will discuss how to update an existing resource using PUT request and different between HTTP PUT and PATCH methods.

REST API HTTP PUT request using Linux curl:

PUT method is used to update or replace an existing resource. If the resource is updated successfully the response will contains status code 200 and if failed, response will contain the respective status codes like 404 or 204 etc.

REST API HTTP PATCH request using Linux curl:

PATCH is similar to PUT request but can update a subset of the resource without replacing the entire content. For example, if you want to update a specific field in your data then use PATCH request and if you want to update the entire content then use PUT request. If the resource is updated successfully the response will contains status code 200 and if failed, response will contain the respective status codes like 404 or 204 etc.

REST API HTTP DELETE request using Linux curl:

How to authorize REST API requests in Linux curl command?

If your REST API endpoint has some authorization mechanism enabled (most have) then you need to authenticate your HTTP request with different authorization schema.

Basic Auth:

It is a simple authentication scheme built into the HTTP protocol. The client sends HTTP requests with the Authorization header that contains the word Basic, followed by a space and a base64-encoded(non-encrypted) string username: password. For example, to authorize as username / Pa$$w0rd the client would send.

Bearer Token:

Commonly known as token authentication. It is an HTTP authentication scheme that involves security tokens called bearer tokens. As the name depicts “Bearer Authentication” gives access to the bearer of this token.

API Key:

An API key is a token that a client provides when making API calls. With API key auth, you send a key-value pair to the API either in the request headers or query parameters. Some APIs use API keys for authorization.

Digest Auth:

Digest Authentication communicates credentials in an encrypted form by applying a hash algorithm to the username and the password, the password is converted to response and then it is sent to the server. After that server supplies nonce value, the HTTP method, and the requested URI.

OAuth:

OAuth permits client applications to access data provided by a third-party API. For example, as a user of a service you can grant another application access to your data with that service without exposing your login details.

Clean up:

Leave a Reply

Close Menu