2

I am making a web application to host JSON data POSTed from an Arduino to be hashed and stored in a database.

I am having an issue with the POST request to the web app being sent from the Arduino but I can't put my finger on it.

The error being received:

[28/Sep/2013:15:43:01 CDT] "POST /json HTTP/1.1" 200 0
- -> /json

[2013-09-28 15:43:06] ERROR bad Request-Line `'.

[28/Sep/2013:15:43:06 CDT] "" 400 0

The Arduino POST:

if (client.connect(server, 4567)) {
 Serial.println("connected");
 // Make a HTTP request:
 client.println("POST /json HTTP/1.1");
 client.println("User-Agent: Arduino");
 client.println("Host: localhost:4567");
 client.print("Accept: *"); client.print("/"); client.println("*");
 client.println("Content-Length: 15");
 client.println("Content-Type: application/x-www-form-urlencoded");
 client.println("");
 client.println("{\"plot\":\"85.1\"}");
 client.println("");
} 

I am using Ruby with Sinatra for the web app.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
 <HEAD><TITLE>Bad Request</TITLE></HEAD>
 <BODY>
 <H1>Bad Request</H1>
 bad Request-Line `'.
 <HR>
 <ADDRESS>
 WEBrick/1.3.1 (Ruby/1.9.3/2013-06-27) at
 localhost:4567
 </ADDRESS>
 </BODY>
</HTML>

Any help would be greatly appreciated, thanks in advance!

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
pdago
  • 107
  • 2
  • 9
  • You don't need that last client.println(""). Not sure if that's enough to cause the error you're seeing. – knolleary Sep 28 '13 at 21:53
  • That did get rid of some of the crazy HTTP response characters but it is still giving the error. I'll add the HTTP response in the orig post. Thanks! – pdago Sep 28 '13 at 21:56

1 Answers1

3

The problem here is the Content-Length value is wrong - you are sending more data than that.

If you remove the last client.println(""); as it isn't needed, and increase Content-Length to either 16 or 17 (depends on what newline chars println adds) then it works.

A more portable approach would be to build the data you want to post in a String object, then set the Content-Length based on its .length().

knolleary
  • 9,777
  • 2
  • 28
  • 35