Returning JSON in Response [Python Code]

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly. In this Python JSON response example, we send a request to the ReqBin echo URL and provide the "Accept: application/json" request header to tell the server that the Python client is expecting JSON. In response to our request, the server sends a JSON response and includes the "Content-Type: application/json" and Content-Length headers, which indicate the type and size of the data in the response body. Click Send to execute Python JSON response example online, and see the results. The Python code was automatically generated for the JSON Response example.
Returning JSON in Response [Python Code] Send
GET /echo/get/json HTTP/1.1
Host: reqbin.com
Accept: application/json

Updated: Viewed: 81657 times
Python code for JSON Response example

Python code for JSON Response Example

This Python code snippet was generated automatically for the JSON Response example.
<< Back to the JSON Response example

What is HTTP?

Hypertext Transfer Protocol is a data transfer protocol between two computers that are used to transfer data between an HTTP client (browser or mobile application) and a server. HTTP is built around messages called "request" and "response". Devices communicate with each other by sending HTTP requests and receiving HTTP responses. All modern programming languages natively support HTTP.

What it JSON?

JavaScript Object Notation (JSON) is a lightweight text-based, language-independent data exchange format. JSON defines a small set of formatting rules for the portable representation of structured data. JSON can represent four primitive types (strings, numbers, boolean values, and null) and two structured types (objects and arrays). JSON is used to exchange data between applications written in many programming languages, including JavaScript, Java, C ++, C #, Go, PHP, Python, and many others.

How to send data in JSON format?

You can send JSON data to the server in the body of an HTTP request and return JSON data from the server in the body of an HTTP response message. In both cases, you must explicitly specify the type of data in the body of the HTTP message using the Content-Type: application/json header (the application/json is the official MIME Type for JSON). The correct Content-Type header allows the server to interpret the data in the client's request correctly and will enable clients to correctly interpret the data in the server's response. Providing the Content-Length header is optional. If you don't know the size of JSON data, you may use chunked transfer encoding for your response.

Content-Type Header Syntax
Content-Type: application/json

JSON Response Example

The following is an example of JSON response when fetching data from ReqBin echo URL:

JSON 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 JSON Response request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.