Skip to content

Get JSON with cURL

Use a standard GET request and, when you want to be explicit about the format you expect, add an Accept header for JSON.

OUTPUT FORMATSHTMLJSONMarkdown

That combination is enough for endpoints that return JSON by default or on request.

The command

Run the command supplied in protected module code0001. It targets an endpoint with a json query parameter and adds a header asking for application/json. Because GET is cURL's default method, you do not need a separate method flag for this task.

Decision checklist

Check these points in order: 1) Confirm the endpoint actually offers JSON, either by default or when requested. 2) Add the Accept header if you want the preference stated explicitly. 3) If the response is not JSON, check whether the server ignored the preference or returned a different format by default. 4) Do not add POST-related options for a retrieval task; that changes the operation rather than the expected response format.

Key terms

cURL is a command-line tool for moving data between a client and a server. It supports protocols such as HTTP and FTP and is commonly used to test APIs, download files and carry out other network tasks. An HTTP GET request asks a server to return a specified resource, such as a page, image or data payload, and the server either supplies it or reports that it cannot be found. JSON is a lightweight, language-independent text format for exchanging data, readable by people and straightforward for programs to parse and generate. An HTTP header travels with the request body and supplies extra context to the server; the Accept header uses that mechanism to state a preferred response format.

Related tasks

For a plain GET, see how to send a GET request using cURL. For sending data instead of retrieving it, see how to POST JSON using cURL. If you need a different response format, compare how to get XML with cURL. For header syntax in general, see how to send an HTTP header using cURL.

Frequently asked questions

Do I need to specify GET explicitly?

No. GET is the default method, so a plain request to the endpoint is enough to start. The Accept header is a preference signal, not a method switch.

Is the Accept header always required?

Often not. Many servers return JSON by default, so omitting the header can still produce JSON. Add it when you want to state the expected format explicitly.

curl {{HTTPBIN_BASE_URL}}/anything?json \
   -H "Accept: application/json"