How to get JSON with cURL?

How to get JSON with cURL?

You can use the cURL command-line tool to make HTTP requests and retrieve JSON data from a URL. Here's a basic example of how to use cURL to get JSON data:


          curl -H "Accept: application/json" -X GET https://example.com/api/data
        

Let's break down this cURL command:


  • curl: The cURL command itself.

  • -H "Accept: application/json": This specifies the HTTP request header, indicating that you expect the response in JSON format. It tells the server that you want to accept JSON data.

  • -X GET: Specifies the HTTP request method. In this case, it's a GET request to retrieve data.

  • https://example.com/api/data: This is the URL from which you want to retrieve JSON data. Replace it with the actual URL you want to fetch JSON from.

When you run this cURL command, it will send a GET request to the specified URL and print the JSON response to the terminal.


You can also save the JSON response to a file by appending the -o option followed by the filename:


          curl -H "Accept: application/json" -X GET https://example.com/api/data -o output.json
        

In this case, the JSON response will be saved to a file named output.json in the current directory. You can replace output.json with your preferred filename.


Remember to replace https://example.com/api/data with the actual URL you want to fetch JSON data from.


Additional Resources:

Web Scraping using curl

How to send basic auth credentials using curl?

How to send HTTP header using cURL?

Scrapingdog logo

Try Scrapingdog for Free!

Free 1000 API calls of testing.

No credit card required!