Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

How to test a REST api from command line with curl In Linux

how-to-test-a-rest-api-from-command-line-with-curl
curl-for-testing-crud-rest-apis
curl-rest

The curl command is a powerful command-line tool for transferring data to or from a server, using various protocols such as HTTP, FTP, SMTP, etc. It is very useful for testing RESTful APIs, as it allows you to simulate HTTP requests and inspect the responses.

Here's an example of how to use the curl command to test a RESTful API:

Assuming that we have a RESTful API endpoint that returns a JSON response with some data, we can use the following curl command to send a GET request and get the response:

curl -X GET https://api.example.com/data
Enter fullscreen mode Exit fullscreen mode

This command sends a GET request to the endpoint https://api.example.com/data. The -X option specifies the HTTP method to use (in this case, GET). The response from the server will be printed to the terminal.

We can also add headers to our request using the -H option, like this:

curl -X GET -H "Authorization: Bearer token" https://api.example.com/data
Enter fullscreen mode Exit fullscreen mode

In this example, we've added an Authorization header with a bearer token to our request.

We can also send data in the body of our request using the -d option, like this:

curl -X POST -H "Content-Type: application/json" -d '{"name": "John", "age": 30}' https://api.example.com/users
Enter fullscreen mode Exit fullscreen mode

This command sends a POST request to the endpoint https://api.example.com/users with a JSON payload in the request body. The -H option specifies the Content-Type header, and the -d option specifies the data to send in the body.

Overall, the curl command is a powerful and versatile tool for testing RESTful APIs. By sending HTTP requests and inspecting the responses, you can ensure that your API is functioning as expected and troubleshoot any issues that may arise.

Top comments (0)