788498
788498

Reputation: 157

New Line definition for HTTP/1.1 headers

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.

Upvotes: 5

Views: 13717

Answers (1)

CodeCaster
CodeCaster

Reputation: 151594

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.

Upvotes: 10

Related Questions