How do I get JSON with Curl? [Python Code]

To get JSON with Curl, you need to make an HTTP GET request and provide the Accept: application/json request header. The application/json request header is passed to the server with the curl -H command-line option and tells the server that the client is expecting JSON in response. If you do not provide an Accept request header, the server may respond with a different MIME type than JSON. The server specifies the returned data type with the Content-Type response header. In this Curl GET JSON example, we send an HTTP GET request to download the JSON from the ReqBin echo URL. Click the Run button to execute the Curl GET JSON example online and see the results. The Python code was automatically generated for the Curl GET JSON example.
How do I get JSON with Curl? [Python Code] Run
   -H "Accept: application/json" 
Updated: Viewed: 46221 times
Python code for Curl GET JSON example

Python code for Curl GET JSON Example

This Python code snippet was generated automatically for the Curl GET JSON example.
<< Back to the Curl GET JSON example

What is Curl?

Curl is a command-line tool that allows developers and administrators to transfer data over the network. Curl supports over 25+ protocols, including HTTP, HTTPS, FTP, and SFTP. When it comes to debugging network requests and API calls, as well as automating your day-to-day routines, Curl is one of the best tools you can find on the market. Curl is versatile, very powerful, works on Linux, Mac, Windows.

What is JSON?

JSON (stands for JavaScript Object Notation) is a lightweight format for storing and transmitting data over a network. JSON is used for client/server communication in web and mobile applications. JSON is self-describing, easy to understand, and language independent.

How to make GET request with Curl?

GET request method is the default HTTP method when requesting a resource from the server. Curl always sends a GET request unless you explicitly tell Curl to use a different HTTP method with the -X command-line option, or you do send data with the --data or --form command-line options. You can also use the --head command-line option to tell Curl to use the HTTP HEAD method instead of the HTTP GET.

Curl GET request syntax

The general form of a Curl command for making a GET request is as follows:

Curl GET Request Syntax
curl [options] URL

  • [options] - some options for the Curl command. For example, you can send an Accept HTTP header to the server with the GET request with -H "Accept: application/json" command-line option.

Why is it important to specify the correct Accept header when getting JSON with Curl?

The server may serve data in different formats from a single API endpoint. In this case, the server looks at the Accept request header and selects the best MIME type for the client. If you don't send the Accept: application/json header with your request, the server may choose to send data of a different type than JSON. To explicitly tell the server to send data in JSON format, we need to add the Accept: application/json header to our request with the Curl -H "Accept: application/json" command-line option.

Can I send JSON in the body of an HTTP GET request?

No, you cannot send JSON and any other data in the body of an HTTP GET request. Sending the body/payload in a GET request message may cause the server to reject the request. If you need to pass data to the server, use URL parameters for this.

Curl GET Request Examples

Below are some Curl examples to get JSON, XML, and HTML data from the server.

How to get JSON data with Curl?

An example of getting JSON data using Curl from the server:

Curl GET JSON Example
   -H "Accept: application/json"

How to get XML data using Curl?

An example of getting XML data using Curl from the server:

Curl GET XML Example
   -H "Accept: application/xml"

How to get the content of an HTML page using Curl?

An example of getting the content of an HTML page using Curl from the server:

Curl GET HTML Example
   -H "Accept: text/html"

See also

Generate code snippets for Python and other programming languages

Convert your Curl GET JSON request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.