How do I get JSON from a REST API endpoint? [Curl/Bash Code]

To receive JSON from a REST API endpoint, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept header tells the REST API server that the API client expects JSON. In this REST API GET example, we make a GET request to the ReqBin echo REST API endpoint. The Content-Type: application/json response header indicates that the REST API server returned JSON data. The Curl/Bash code was automatically generated for the REST API GET example.
How do I get JSON from a REST API endpoint? [Curl/Bash Code] Send
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Updated: Viewed: 31723 times
Curl/Bash code for REST API GET example

Curl/Bash code for REST API GET Example

This Curl/Bash code snippet was generated automatically for the REST API GET example.
<< Back to the REST API GET example

What is JSON?

JavaScript Object Notation (JSON) is a text-based, language-independent format for storing and transmitting data over a network. JSON is used for client/server communication in mobile and web applications written in many programming languages, including JavaScript, Python, Java, C++, C#, Go, PHP, and many others.

What is REST API?

A REST API (Representative State Transfer) is a way of communicating two computers over the Internet. The REST API defines a set of rules that developers should follow when creating an API:

  1. Client-server architecture: the REST API client (website or mobile app) should be decoupled from the request processor and data storage (REST API server) so that each part can be developed, tested, and scaled individually.
  2. Statelessness: every request to the REST API server must be made with all the required data, and Rest API should not assume if the server has any data from previous REST API client requests (the server does not store the client context).
  3. Layered: the REST API client does not need to know if it communicates with a real server, proxy, or other intermediaries. These intermediate servers (proxies or load balancers) can provide the underlying REST API server with additional scalability and security.
  4. Cacheable: each REST API response must be defined as cacheable or not.

REST API GET Example

To get data from the REST API server, you must send an HTTP GET request with an Accept: application/json request header. Below is an example of a REST API GET request to the ReqBin REST API endpoint.

REST API GET Request
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

In this REST API GET request example, the server informs the REST API client that it has returned JSON by sending Content-Type: application/json in response.

The server response to our test REST API GET request.

REST API Server Response
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19
      
{"success":"true"}

See also

Generate code snippets for Curl/Bash and other programming languages

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