Node.js code for HTTP Request And Response Example
This Node.js 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:
- The HTTP method. Methods are one-word commands that tell the server what to do with resources.
- The URL path component for the request. A path identifies a resource on a server.
- The HTTP version number. Displaying the HTTP specification.
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.
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:
- The HTTP version number. Displays the HTTP specification.
- The status code, which indicates the result of the request.
- The reason phrase or status text summarizes the meaning of a status code in human-readable text.
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.
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:
The following is an HTTP Response example to the ReqBin echo URL: