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 PHP code was automatically generated for the Curl GET JSON example.
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?
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.
An example of getting JSON data using Curl from the server: