New Line definition for HTTP/1.1 headers

隐身守侯 提交于 2019-12-10 10:41:18

问题


I have an embedded system that makes a HTTP POST request, however I know that the headers must have a format.

In this case I have this request:

POST / HTTP/1.1\n
Host: 192.168.1.15\n
Connection: close\n
Content-Length: 44\n
Content-Type: application/json\n
\n
{\n
"command": "snapPicture",\n
"selfTimer": 0\n
}

I want to avoid any kind of error while I send this request.

  1. Strictly talking, is it correct use \n to tell new line or should be \r\n?

  2. Any suggestion about this request format?

Thanks for your help.


回答1:


You must use \r\n to separate headers, also to separate the body from the header section. See RFC 7230 (Hypertext Transfer Protocol (HTTP/1.1): Message Syntax and Routing), section 3. Message Format:

HTTP-message   = start-line
                  *( header-field CRLF )
                  CRLF
                  [ message-body ]

CRLF being \r\n.

Whether the rest of the request is valid, especially the body, depends on the server you send it to.



来源:https://stackoverflow.com/questions/27966357/new-line-definition-for-http-1-1-headers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!