2

I have been stuck on a small piece of code trying it out.

            char httpRequest[] = "POST http://myhost.com/ HTTP/1.1\r\n"
            "Host: myhost.com\r\n"
            "Content-Type: application/x-www-form-urlencoded\r\n"
            "Content-Length: 10\r\n"
            "Connection: close\r\n"
            "\r\n"
            "text1=test";

sock_error2 =  send(tcpsock, httpRequest, strlen(httpRequest) + 1, 0);

The request never reaches the destination server, i have packet dump on both sides.

Whenever i change content-length to 0, the packet is received by the server without the body of course. What am i doing in wrong in content-length or carriage returns?

  • 1
    Conent-Length: 10 shall probably correlate with "text1=test", which is 10 bytes long, you add 1 byte-length to your request with "strlen(httpRequest) + 1" which *could* be a problem for the receiver... – yussuf Aug 13 '20 at 09:58
  • Like yussuf said, strlen returns the length of string from the pointer until a terminating nullbyte without including the null byte itself, by adding 1 to the length, you're also sending over this nullbyte, however, the server *should* ignore this character since it *should* read only the specified byte count in the `Content-Length` header – Expolarity Sep 28 '20 at 19:30
  • As for your carriage returns and newlines, I don't see anything wrong with those, there should not be a required \r\n after the body (i never seen it included in actual HTTP requests or responses) – Expolarity Sep 28 '20 at 19:32

0 Answers0