REST API Example [Python Code]

To send a request to the REST API endpoint, you must enclose a JSON data in the request body and indicate the data type with the "Content-Type: application/json" request header. The client also can send the "Accept: application/json" request header, which indicates that the client wants to receive the data in JSON format. If the REST API server returns a JSON, it indicates the type of data in response with the "Content-Type: application/json" response header. In this REST API Example, we are sending JSON data to the ReqBin echo URL with Accept: application/json HTTP header. Click Send to make a REST API request online and see the results. The Python code was automatically generated for the REST API example.
REST API Example [Python Code] Send
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 42

{"Id": 78912,"Quantity": 1,"Price": 18.00}
Updated: Viewed: 47178 times
Python code for REST API example

Python code for REST API Example

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

What is REST API?

The REST API is an application programming interface created in 2000 by computer scientist Roy Fielding. A REST API is an API that follows REST design principles or an architectural style of representational state transfer that provides a relatively high level of flexibility and freedom for developers. This flexibility is just one of the reasons why REST APIs have become the standard way to connect components and applications in a microservice architecture. For this reason, REST APIs are sometimes referred to as RESTful APIs. REST APIs communicate via HTTP requests to perform standard CRUD (read, update, and delete) operations within a resource. For example, a REST API will use a GET request to retrieve a document, a POST request to create it, a PUT request to update a record, and a DELETE request to delete it. API calls can use all HTTP methods.

What is an API?

API (Application Programming Interface) are definitions and protocols for building and integrating application software. An API is a software mediator that allows two applications to exchange data. The APIs are standards-compliant (HTTP and REST) that are user-friendly and readily available to developers. The API has its software development lifecycle (SDLC), which includes designing, testing, building, managing, and versioning, just like any other software produced. In addition, the APIs are well documented for use and version control.

What is REST?

REpresentational State Transfer (REST) is an architectural style that enforces standards between computer systems on the Internet to make it easier for systems to interact with each other. Like other architectural styles, REST has its own rules and limitations. REST systems often referred to as RESTful systems, are separate stateless client and server tasks.

When the API is considered RESTful?

For an API to be considered RESTful, it must meet the following criteria:

  • The client-server architecture consists of clients, servers, and resources, and requests are managed over HTTP.
  • Cached data that simplifies the exchange of data between client and server.
  • Stateless communication on the client-server, which means no client information, is kept between GET requests, and each request is separate and unrelated.
  • A multi-tiered system that organizes servers of each type (responsible for security, load balancing)

How to send a REST API request?

To send a REST API request to a server with JSON data, you must include the data in the body of the HTTP message and set the appropriate content type for the request body. Your client should also send the Accept: application/json request header if it expects JSON data from the server. Below is an example of sending a REST API request:

REST API Requests Example
POST /echo/post/json HTTP/1.1
Host: reqbin.com
Accept: application/json
Content-Type: application/json
Content-Length: 42

{"Id": 78912,"Quantity": 1,"Price": 18.00}

REST API Responses Example

Below is an example of a REST API server response to our test request:

REST API Responses Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19

{"success":"true"}

The Content-Type: application/json response header informs the client that the server has returned JSON data. The Content-Length: 19 header indicates the size of JSON in response.

See also

Generate code snippets for Python and other programming languages

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