Html http请求正文是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22034144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does it mean http request body?
提问by Vignesh Gopalakrishnan
回答by Mazzu
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).
HTTP 正文数据是在 HTTP 事务消息中传输的数据字节,紧跟在标头之后(如果有的话)(在 HTTP/0.9 的情况下不传输标头)。
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.
大多数 HTTP 请求是没有正文的 GET 请求。但是,使用主体模拟请求对于正确强调代理代码并测试处理此类请求的各种钩子很重要。大多数带有正文的 HTTP 请求使用 POST 或 PUT 请求方法。
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.
消息正文部分对于 HTTP 消息是可选的,但如果它可用,则它用于携带与请求或响应关联的实体正文。如果关联实体主体,则通常 Content-Type 和 Content-Length 标题行指定关联主体的性质。
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:
消息体是承载实际 HTTP 请求数据(包括表单数据和上传等)和来自服务器的 HTTP 响应数据(包括文件、图像等)的消息体。以下是消息正文的简单内容:
<html>
<body>
<h1>Hello, World!</h1>
</body>
</html>
For more details to HTTP messages and bodies refer to w3org link
有关 HTTP 消息和正文的更多详细信息,请参阅w3org 链接
回答by August Jelemson
The following html <form>
:
以下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):
将发送此HTTP 请求(这是一种HTTP 消息):
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).
线POST / HTTP/1.1
到Content-Length: 465
是HTTP报头,而其余的-以下的空线-对应于HTTP消息体(也称为内容)。
So how do you access this data in the back-end/server-side?
Different server-languages (e.g. Go-lang, Node.js and PHP... etc) have different ways to parse the http body
from a http post request
. In Node.js it is common to use body-parserwhich is a parsing middleware function (see example below).
那么如何在后端/服务器端访问这些数据呢?
不同的服务器语言(例如 Go-lang、Node.js 和 PHP 等)有不同的方法来解析http body
来自http post request
. 在 Node.js 中,通常使用body-parser,它是一个解析中间件函数(参见下面的示例)。
// Node.js
// OBSERVE THAT 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:
- Single-resource bodies, consisting of one single file, defined by the two headers: Content-Typeand Content-Length.
- Multiple-resource bodies, consisting of a multipart body, each containing a different bit of information. This is typically associated with HTML Forms.
- 单一资源主体,由一个文件组成,由两个标头定义:Content-Type和Content-Length。
- 多资源主体,由多部分主体组成,每个主体包含不同的信息位。这通常与HTML Forms相关联。
Sources:
资料来源:
回答by mikebrooks
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:
一个常见用例是需要 JSON 格式数据的 API。下面是摘自 Postman 的示例代码片段,其中 API 是 Azure 函数,请求正文是 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"
}