HTTP Request and Response Example [Python Code]

In client-server communication, clients send HTTP requests to the server and get HTTP responses. With HTTP GET requests, a client may request resources from a server. When sending an HTTP request, the client can provide additional information to the server, such as cookies or authorization data. Submitting data from web forms or uploading images to the server is done using HTTP POST requests. The servers send the requested resources to the client with HTTP responses. If the client requests the server to perform some operation, then with the HTTP response, the server returns the result of the operation or informs about the error. In this HTTP Request and Response example, we send an HTTP request to the ReqBin echo URL and receive an HTTP response from the ReqBin server. Click Send to execute the HTTP Request and Response example online and see the results. The Python code was automatically generated for the HTTP Request And Response example.
HTTP Request and Response Example [Python Code] Send
GET /echo HTTP/1.1
Host: reqbin.com
Accept: text/html

Updated: Viewed: 33329 times
Python code for HTTP Request And Response example

Python code for HTTP Request And Response Example

This Python code snippet was generated automatically for the HTTP Request And Response example.
<< Back to the HTTP Request And Response example

What is HTTP?

The HTTP (Hypertext Transfer Protocol) is an Internet communication protocol widely used for transferring data between a client and a server. For data transfer, the HTTP protocol assumes client-server architecture. A client application sends a request to the server, and the server processes the request and returns a response.

HTTP Request Structure

The HTTP request contains the following elements:

Request line

The request line is the first line in the request message, and it contains three items:

  1. The HTTP method. Methods are one-word commands that tell the server what to do with resources.
  2. The URL path component for the request. A path identifies a resource on a server.
  3. The HTTP version number. Displaying the HTTP specification.
HTTP Request Line Example
GET /echo HTTP/1.1

HTTP Request Headers

HTTP headers are specified to the message to supply the recipient with information about the message, the sender, and the way the sender wants to communicate with the recipient. Each HTTP header consists of a "key: value" pair. The HTTP headers for a client request contain information that the server can use to decide how to respond to the request.

HTTP Request Headers Example
Content-Type: application/json

Request Message Body

The request message body contains the body of the object, which may be in its original state or encoded. Message bodies are suitable for some request methods and not suitable for others. For example, a request with an HTTP POSTT method that sends input to the server has a message body containing the data. A request with an HTTP GET method that asks the server to send a resource does not have a message body.

HTTP Response Structure

The HTTP response contains the following elements:

Status line

The status line is the first line in the request message, and it contains three items:

  1. The HTTP version number. Displays the HTTP specification.
  2. The status code, which indicates the result of the request.
  3. The reason phrase or status text summarizes the meaning of a status code in human-readable text.
HTTP Response Status Line Example
HTTP/1.1 200 OK

HTTP Response Headers

The HTTP headers for a server response contain information that a client can use to learn more about the response and the server that sent it. This information can help the client display a response to the user and store (or cache) the response for future use. If a request fails, headers can tell the client what it needs to do to succeed.

HTTP Response Headers Example
Content-Type: text/html; charset=utf-8

Response Message body

The message bodies are used for most responses. The exceptions are when the server responds to a client request using the HTTP HEAD method (which requests headers but not the response body) and when the server uses specific status codes.

HTTP Request and Response Example

The following is an HTTP Request example to the ReqBin echo URL:

HTTP Request Example
GET /echo HTTP/1.1
Host: reqbin.com
Accept: text/html

The following is an HTTP Response example to the ReqBin echo URL:

HTTP Response Example
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Connection: keep-alive

{html data}

See also

Generate code snippets for Python and other programming languages

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