Getting JSON from a REST API Endpoint [Python Code]

To get JSON from a REST API endpoint using Python, you must send an HTTP GET request to the REST API server and provide an Accept: application/json request header. The Accept: application/json header tells the REST API server that the API client expects to receive data in JSON format. The Content-Type: application/json response header indicates that the REST API server returned data in JSON format. In this Python REST API GET JSON example, we make a GET request to the ReqBin REST API endpoint. Click Send to execute the Python REST API GET JSON request online and see the results. The Python code was automatically generated for the REST API GET example.
Getting JSON from a REST API Endpoint [Python Code] Send
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Updated: Viewed: 77561 times
Python code for REST API GET example

Python code for REST API GET Example

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

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.

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.

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. The server informs the client that it has returned JSON by sending "Content-Type: application/json" in response. The following is an example of a REST API GET request to the ReqBin API endpoint:

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

The server response to our test REST API GET request:

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

See also

Generate code snippets for Python and other programming languages

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