To get JSON with Curl, you need to make a 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 data. If you do not provide this 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. The PHP code was automatically generated for the Curl GET JSON example.
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.
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 parameter.
Curl GET Request Examples
Below are some Curl examples to get JSON, XML and HTML data from the server.