New Line definition for HTTP/1.1 headers

788498 picture 788498 · Jan 15, 2015 · Viewed 7.9k times · Source

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.

Answer

CodeCaster picture CodeCaster · Jan 15, 2015

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.