curl get request example

Curl GET Request Example

As a web developer, I often use the command line tool Curl to test APIs and other web services. Curl is a powerful tool that allows you to send HTTP requests using various methods such as GET, POST, PUT, DELETE, etc.

In this example, I will demonstrate how to use Curl to make a GET request to a web API and retrieve data from it.

Step 1: Open Command Line

First, open your command line interface. On Windows, this is usually the Command Prompt. On macOS or Linux, this is typically the Terminal.

Step 2: Enter Curl Command

To make a GET request using Curl, we need to use the following syntax:

$ curl [URL]

Replace [URL] with the URL of the API you want to access. For example, if we want to access the JSONPlaceholder API which provides fake data for testing purposes, we would use:

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

This will return data for the first post in the API in JSON format.

Step 3: Add Additional Options

You can add additional options to your Curl command to modify the request. For example, you can add a -H option to specify HTTP headers:

$ curl -H "Authorization: Bearer [TOKEN]" https://api.example.com/data

This will add an Authorization header with a bearer token to the request.

You can also add a -d option to send data with the request:

$ curl -d "name=John&age=30" https://api.example.com/users

This will send a POST request to the API with the data "name=John&age=30".

Step 4: View Response

After making your Curl request, you will receive a response from the server. By default, Curl displays the response in the command line interface.

You can also save the response to a file using the -o option:

$ curl -o response.json https://api.example.com/data

This will save the response to a file named "response.json".

Conclusion

Curl is a powerful tool for testing web APIs and services. By using Curl, you can easily make HTTP requests and receive responses directly in your command line interface. With additional options, you can modify your requests and get the data you need.