5

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
  • Many servers will return 400 Bad request if you use only the line feed (\n) or only carriage return (\r) char. Jun 16, 2022 at 5:50

1 Answer 1

10

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.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.