简体   繁体   English

标头和正文在http中的响应和请求的顺序是什么?

[英]What is the order of header and body in response and request in http?

I was just reading a Wikipedia article about "list of HTTP header fields " an come to this line which says;"the header field are transmitted after the request or response line, which is the first line of a message ". 我只是在阅读Wikipedia上有关“ HTTP标头字段列表”的文章,并在该行中说:“标头字段是在请求或响应行(即消息的第一行)之后传输的”。 Does it means when writing a program let's say a CGI program we first send the response body and headers should be placed at the bottom of the CGI script. 这是否意味着在编写程序时,例如我们首先发送响应主体,并且标头应放在CGI脚本的底部,这是一个CGI程序。 But based on my experience with CGI program in python , we used to write header first and then write some CRLF and after that we write response body. 但是根据我在python中使用CGI程序的经验,我们通常先编写标头,然后编写一些CRLF,然后再编写响应正文。

Can some please help me here to understand correct order of body and headers in http ? 可以请一些人帮我理解http中正文和标题的正确顺序吗?

Does it means when writing a program let's say a CGI program we first send the response body and headers should be placed at the bottom of the CGI script. 这是否意味着在编写程序时,例如我们首先发送响应主体,并且标头应放在CGI脚本的底部,这是一个CGI程序。

No this is incorrect. 不,这是不正确的。

the header field are transmitted after the request or response line , 标头字段在请求或响应之后传输,

Notice the emphathized "line". 请注意强调的“行”。 The request line is simply something like GET /index.html HTTP/1.1 and is the first line of each request. 请求行就像GET /index.html HTTP/1.1 ,是每个请求的第一行。 The headers are transmitted after this, and then after the headers come the body. 在此之后,将发送标头,然后在标头到达正文之后进行发送。

If you have access to curl (OS X or Linux terminal), try something like curl -v http://www.google.com . 如果您可以使用curl(OS X或Linux终端),请尝试使用curl -v http://www.google.com On my machine you see the order clearly: 在我的机器上,您可以清楚地看到订单:

Request: 请求:

> GET / HTTP/1.1
> User-Agent: curl/7.38.0
> Host: www.google.com
> Accept: */*
> 

The body is always empty for a GET request, but a POST you would see the data sent. 对于GET请求,主体始终为空,但是在POST中,您将看到发送的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM