HTTP 400 - Hard to understand error code with minimal description


November 2018


477 time



My requirement is fairly simple. I have to perform a simple HTTP POST to an IP:port combination. I used simple socket programming to do that and I have been successful in sending across my request to them and also get back response from them. The only problem being that the response is always a HTTP 400: Bad Request followed by my HTTP POST message. I am not sure if the problem is with the client or the server. My only guess being that there might be a problem with my data that I am sending. This is what my POST looks like

POST /<Server Tag> HTTP/5.1
Content-Length: xxx


and the response from the server looks something like this

HTTP/1.1 400 Bad Request
Content-Length: xxx

--Same content that I sent them--

I was not sure If I could put in the IP of the server here so kept myself to using . I am pretty sure that the problem would not be there since I get back some response from the server and confident about the connection. Can someone help me ?

PS: Some pointers about my POST:

1) HTTP 5.1 was requested by the server and I am not sure if that is correct
2) I have played around with the number of line spaces after the content length. I have tried giving one and two lines. Not sure if that would make a difference. On wireshark though I see a difference with the number of line spaces as with a single line space the protocol is specified as TCP but with two it changes to HTTP. The response is always received on HTTP protocol. Some explanation on the difference would also help


edit: the other thing that confuses me is that the response has a HTTP 1.1 and not a 5.1 that I had sent. I have also tried changing my post to 1.1 with no success

edit2: Based on suggestion form fvu and others, I used WebClient to Upload my request. Still got back a 400. The header that was generated by the WebClient looks like this

POST <server tag> HTTP/1.1
Host: <IP:PORT>
Content-Length: 484
Expect: 100-continue
Connection: Keep-Alive

The issue I see with this might be that the server was not expecting all the details in the header. The server has requested only the Content-Length from us. Would that be a problem? Thanks


2 answers

  1. HTTP / 5.1 либо неправильно или неправильно программистом приложения сервера

  2. Вы должны получить правильный пример из API сервера, чтобы проверить реализацию протокола первой.


Вы можете использовать отладочный прокси-сервер для просмотра запроса клиента и ответа сервера, чтобы выяснить, что сокет клиента программа должна делать.

Но сначала нужно создать простой веб-страницы, что браузер отображает, позволяет сделать POST от браузера к веб-серверу, и получить простой ответ от сервера.