Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean http request body?

Upon reading stuffs about POST and get methods here there is a statement like " when used post method it uses HTTP request Body . What Does it mean " HTTP request body".?

like image 575
Vignesh Gopalakrishnan Avatar asked Feb 26 '14 07:02

Vignesh Gopalakrishnan


People also ask

What is HTTP request header and body?

The start-line and HTTP headers of the HTTP message are collectively known as the head of the requests, whereas its payload is known as the body.

Do HTTP requests have a body?

Yes. In other words, any HTTP request message is allowed to contain a message body, and thus must parse messages with that in mind. Server semantics for GET, however, are restricted such that a body, if any, has no semantic meaning to the request.

Why do we use request body?

Simply put, the @RequestBody annotation maps the HttpRequest body to a transfer or domain object, enabling automatic deserialization of the inbound HttpRequest body onto a Java object. Spring automatically deserializes the JSON into a Java type, assuming an appropriate one is specified.

What should request body include?

Request bodies are typically used with “create” and “update” operations (POST, PUT, PATCH). For example, when creating a resource using POST or PUT, the request body usually contains the representation of the resource to be created. OpenAPI 3.0 provides the requestBody keyword to describe request bodies.


3 Answers

HTTP Body Data is the data bytes transmitted in an HTTP transaction message immediately following the headers if there is any (in the case of HTTP/0.9 no headers are transmitted).

Most HTTP requests are GET requests without bodies. However, simulating requests with bodies is important to properly stress the proxy code and to test various hooks working with such requests. Most HTTP requests with bodies use POST or PUT request method.

Message Body

The message body part is optional for an HTTP message but if it is available then it is used to carry the entity-body associated with the request or response. If entity body is associated then usually Content-Type and Content-Length headers lines specify the nature of the body associated.

A message body is the one which carries actual HTTP request data (including form data and uploaded etc.) and HTTP response data from the server ( including files, images etc). Following is a simple content of a message body:

<html> <body> <h1>Hello, World!</h1> </body> </html> 

For more details to HTTP messages and bodies refer to w3org link

like image 177
Mazzu Avatar answered Oct 11 '22 20:10

Mazzu


The following html <form>:

<form action="http://localhost:8000/" method="post" enctype="multipart/form-data">   <label>Name: <input name="myTextField" value="Test"></label>   <label><input type="checkbox" name="myCheckBox"> Check</label>   <label>Upload file: <input type="file" name="myFile" value="test.txt"></label>   <button>Send the file</button> </form> 

will send this HTTP request (which is a type of HTTP message):

POST / HTTP/1.1 Host: localhost:8000 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Language: en-US,en;q=0.5 Accept-Encoding: gzip, deflate Connection: keep-alive Upgrade-Insecure-Requests: 1 Content-Type: multipart/form-data; boundary=---------------------------8721656041911415653955004498 Content-Length: 465  -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myTextField"  Test -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myCheckBox"  on -----------------------------8721656041911415653955004498 Content-Disposition: form-data; name="myFile"; filename="test.txt" Content-Type: text/plain  Simple file. -----------------------------8721656041911415653955004498-- 

Lines POST / HTTP/1.1 to Content-Length: 465 are the HTTP headers, whilst the rest -- following the empty line -- corresponds to the HTTP message body (also known as content).

So how do you access this data in the back-end/server-side?
Different server-languages (e.g. Go-lang, Node.js, PHP... etc) have different ways to parse the http body from a http post request. In Node.js it is common to use body-parser which is a parsing middleware function (see example below).

// Node.js // OBSERVE: YOU NEED THE BODY-PARSER MIDDLEWARE IN ORDER TO DO THIS! ⋮ var data1 = req.body.myTextField; var data2 = req.body.myCheckBox; var data3 = req.body.myFile; ⋮ 

More information about bodies:

Bodies can be broadly divided into two categories:

  1. Single-resource bodies, consisting of one single file, defined by the two headers: Content-Type and Content-Length.
  2. Multiple-resource bodies, consisting of a multipart body, each containing a different bit of information. This is typically associated with HTML Forms.

Sources:

  • MDN
like image 37
August Jelemson Avatar answered Oct 11 '22 19:10

August Jelemson


A common use case is an API that expects data in JSON format. Below is an example code snippet taken from Postman, where the API is an Azure Function and the request body is JSON:

POST /api/ValidateTwitterFollowerCount HTTP/1.1
Host: myazurefunction.azurewebsites.net
Content-Type: application/json
cache-control: no-cache
Postman-Token: XXXXXXX-XXXXX-XXXXXX

{
    "followersCount" : 220,
    "tweettext":"#Stack Overflow rocks",
    "Name": "John Doe"
}
like image 32
mikebrooks Avatar answered Oct 11 '22 20:10

mikebrooks