Sending HTTP POST JSON requests and "400 BAD REQUEST" replies

Hi all

I’m trying to send a HTTP POST request to a webserver

The string being sent to the 3G modem (SIMCOM 5216) is:

"POST /readings HTTP/1.1\r\nHost:\r\nUser-Agent: Arduino/1.0\r\nAccept: */*\r\nContent-Type: application/json\r\nContent-Length: 66\r\n\r\n\r\n{\"site_id\":2,\"temp_avg\":11,\"rh_avg\":22,\"wind_avg\":33,\"alert\":true}\x1a"

Which results in the following in the terminal:

POST /readings HTTP/1.1
User-Agent: Arduino/1.0
Accept: */*
Content-Type: application/json
Content-Length: 66


and gets the following response from the webserver:

http/1.1 400 bad request
server: nginx/1.6.2
date: sat, 06 dec 2014 07:29:41 gmt
content-type: text/html
content-length: 172
connection: close

<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>

I know the module has been correctly configured to connect to the network (ie email works fine).
I know the module has established the http connection to the server (ie correct response to the setup AT command and the fact we get SOME reply from the server)
What has me stumped is the reply from the server - if we use CURL to send the same JSON request it works, but for some reason its rejecting the request sent via the module.

Any guru’s able to suggest where to look - I know its not really an Arduino question!

Try removing a set of '\r\n' between the message header and message body. Two line breaks indicates the end of the header if I recall correctly, you have three.

This is from the HTTP/1.1 spec: Certain buggy HTTP/1.0 client implementations generate extra CRLF's after a POST request. To restate what is explicitly forbidden by the BNF, an HTTP/1.1 client MUST NOT preface or follow a request with an extra CRLF.

Turned out to be the http:// on the HOST line…annoying!


Turned out to be the http:// on the HOST line............annoying!

Why is that annoying? The NAME of the host does not involve the protocol used to talk to that host. You make it sound like it's someone else's fault that you made a mistake.

No PaulS - the datasheet & appnote was VERY unclear as to the correct format for the commands actually! Even the web developer said it looked like it meant you had to include the http:// in the AT command even though that was wrong according to the the standards.

The server rejection message was very ambiguous and it wasnt until we maxed out the logging detail we saw that was the issue and resolved it.

We also identified a bug int he module FW where the AT command is accepted with the http:// included IF there is only ONE lot of CR/LF after the header - but of course it fails with Error 400 due to the http://.

If you remove the http:// but still have only ONE (or three!) lot(s) of CF/LF the command wont run at all. Once you have TWO lots of CR/LF between the header and data and no http:// it works fine

So it was a combination of our incorrect use of the http:// in the host command (from poor datasheet/appnote) and too many (or not enough!) CR/LF as per pYro_65's post that was the full issue

"POST /readings HTTP/1.1\r\nHost:\r\nUser-Agent: Arduino/1.0\r\nAccept: /\r\nContent-Type: application/json\r\nContent-Length: 66\r\n\r\n\r\n{\"site_id\":2,\"temp_avg\":11,\"rh_avg\":22,\"wind_avg\":33,\"alert\":true}\r\n\r\n\x1a"

the request should end with a double newline before (ctrl+z).

dpulidindi, it does not work for me : 400 http error

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.