HTTP/1.1 400 Bad Request | GitHub Installation | Forum

Avatar

Please consider registering
Guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed sp_TopicIcon
HTTP/1.1 400 Bad Request
No permission to create posts
March 26, 2014
2:30 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

Hi all, I tried to have some fun and installed Thingspeak API on Ubuntu. I followed the clean install procedure on GitHub and all went well. I created an account and a channel. I was able to send values via web browser too. Then I took my arduino that I use to post to thingspeak, changed the sketch for my server's IP address, and changed the port to 3000, I get :

Connected to ThingSpeak...

HTTP/1.1 400 Bad Request
Content-Type: text/plain
Connection: close
Server: thin 1.6.1 codename Death Proof

In my sketch, do I have to modify anything else from my original code other than thingSpeakAddress, API Key and port 80 to 3000? Here is my post:

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");

I am sure that it is something small since I've gotten this far. Thanks

March 26, 2014
4:19 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi,

That seems to be everything you would need to change in the code. Also make sure the devices are on the same network, and that the IP address of the server running ThingSpeak is accessible from another computer on your network.

lee

March 26, 2014
7:47 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

lee said

Hi,

That seems to be everything you would need to change in the code. Also make sure the devices are on the same network, and that the IP address of the server running ThingSpeak is accessible from another computer on your network.

lee

Thanks lee, they are both on the same switch and both connected to the internet. The server is at 132.206.95.88:3000
From other computers I was able to post data via Firefox. Bad request for me means my post info is wrong. Wouldn't Server connection issues probably give me 404 error?

March 26, 2014
9:24 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi,

Yes you're right the 400 Bad Request probably means the connection is fine. Are you able to look at the Rails server log while it's running and see what the error is?

lee

March 26, 2014
10:14 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

Still a real newbie at the server stuff, will try to find the log in the morning. Will let you know. Thanks

Gerald

March 27, 2014
11:33 am
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

I fould the error in the log!!! Have no clue how to fix it though...Any idea Lee?

Invalid request: Invalid HTTP format, parsing fails.
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/thin-1.6.1/lib/thin/request.rb:84:in `execute'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/thin-1.6.1/lib/thin/request.rb:84:in `parse'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/thin-1.6.1/lib/thin/connection.rb:41:in `receive_data'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/thin-1.6.1/lib/thin/backends/base.rb:73:in `start'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/thin-1.6.1/lib/thin/server.rb:162:in `start'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/rack-1.5.2/lib/rack/server.rb:264:in `start'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/railties-4.0.3/lib/rails/commands/server.rb:84:in `start'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/railties-4.0.3/lib/rails/commands.rb:76:in `block in '
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/railties-4.0.3/lib/rails/commands.rb:71:in `tap'
/home/gerald/.rvm/gems/ruby-2.1.0@thingspeak/gems/railties-4.0.3/lib/rails/commands.rb:71:in `'
script/rails:6:in `require'
script/rails:6:in `'

March 27, 2014
12:04 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

Also the Content-Type is troubling me. In my sketch I state:
client.print("Content-Type: application/x-www-form-urlencoded\n");

But the server replies to me:

HTTP/1.1 400 Bad Request
Content-Type: text/plain
Connection: close
Server: thin 1.6.1 codename Death Proof

March 27, 2014
2:53 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Are you sending the request via HTTP or HTTPS? Can you paste your modified Arduino code here so I can look at it?

March 27, 2014
3:05 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

Should be HTTP

// Local Network Settings
byte mac[] = { 0xD4, 0x28, 0xB2, 0xFF, 0xCA, 0x21 }; // Must be unique on local network, last digit matches static ip
byte ip[] = {XXX, XXX, 95, 81 };
byte dnsserver[] = {XXX, XXX, 85, 18 }; //This is necessary for McGill static ip network
byte gateway[] = {XXX, XXX, 95, 253 };

// ThingSpeak Settings
byte thingSpeakAddress[] = {XXX, XXX 95, 88 };
String writeAPIKey = "JJKUVNXUIXXXXXXX"; // API String for required channel
const int updateThingSpeakInterval = 20 * 1000; // Time interval in milliseconds to update ThingSpeak (number of seconds * 1000 = interval) 5 Mins

// Variable Setup
long lastConnectionTime = 0;
boolean lastConnected = false;
int failedCounter = 0;

// Initialize Arduino Ethernet Client
EthernetClient client;

void setup(){
Ethernet.begin(mac, ip, dnsserver, gateway);
dht.begin();

// Open serial communications and wait for port to open:
Serial.begin(9600);

delay(2000);
}

void loop()
{

// Print Update Response to Serial Monitor
if (client.available())
{
char c = client.read();
Serial.print(c);
}

// Disconnect from ThingSpeak
if (!client.connected() && lastConnected)
{
Serial.println("...disconnected");
Serial.println();

client.stop();

}

// Update ThingSpeak
if(!client.connected() && (millis() - lastConnectionTime > updateThingSpeakInterval))
{
// Read the value fromn the DHT22 sensor
int sensorValue = dht.readTemperature();
int sensorValue2= dht.readHumidity();
// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
int temp = sensorValue; // offset can be added here for temperature
int humd = sensorValue2; // offset can be addded here for humidity
delay(250000);
updateThingSpeak("field1="+String(temp, DEC)+"&field2="+String(humd, DEC)); // Modify necessary field numbers depending on what chart you are inputing to
}

lastConnected = client.connected();

}
void updateThingSpeak(String tsData)
{
if (client.connect(thingSpeakAddress, 3000))
{
Serial.println("Connected to ThingSpeak...");
Serial.println();

client.print("POST /update HTTP/1.1\n");
client.print("Host: XXX.XXX.95.88\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");

client.print(tsData);

lastConnectionTime = millis();

failedCounter = 0;

}
else
{
//Serial.println("Connection Failed.");
Serial.println("Connection to ThingSpeak failed ("+String(failedCounter, DEC)+")");
Serial.println();

failedCounter++;

if (failedCounter >=3 ) {resetEthernetShield();}

lastConnectionTime = millis();
}
}

void resetEthernetShield()
{
Serial.println("Resetting Ethernet Shield.");
Serial.println();

client.stop();
delay(1000);

Ethernet.begin(mac, ip, dnsserver, gateway);
delay(1000);

March 27, 2014
7:54 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

In the line:
client.print("Host: XXX.XXX.95.88\n");
can you try adding the port afterwards, like:
client.print("Host: XXX.XXX.95.88:3000\n");

If that doesn't work, try stopping the server (CTRL-c) and restarting it using an alternative server via `rails server webrick` to see if that can offer any more insight into the errors.

If I just use my browser and go to "http://localhost:3000/channels%c" I'm able to reproduce the same error you're getting, so you may want to double-check the Arduino code to make sure that all characters are escaped properly ("%c" in my example is invalid).

lee

March 28, 2014
11:35 am
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

I tried adding the :3000 before and confirmed that this did nothing. Something really strange though, it WORKS! If start the rails server with the webrick:

Connected to ThingSpeak...

HTTP/1.1 200 OK
X-Frame-Options: ALLOWALL
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
Content-Type: text/html; charset=utf-8
Etag: "c4ca4238a0b923820dcc509a6f75849b"
Cache-Control: max-age=0, private, must-revalidate
X-Request-Id: 231c2c64-9338-49cc-85f2-cdb0f4147f58
X-Runtime: 0.116638
Server: WEBrick/1.3.1 (Ruby/2.1.0/2013-12-25)
Date: Fri, 28 Mar 2014 15:33:32 GMT
Content-Length: 1
Connection: close

1...disconnected

March 28, 2014
1:43 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

I'm not sure what the problem is then with our default development server (called "Thin"), but I'm glad Webrick works for you. If you need a more production-quality server, you can use Phusion Passenger (with Nginx), which is what we use on thingspeak.com.

lee

March 28, 2014
1:57 pm
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

lee said

I'm not sure what the problem is then with our default development server (called "Thin"), but I'm glad Webrick works for you. If you need a more production-quality server, you can use Phusion Passenger (with Nginx), which is what we use on thingspeak.com.

lee

Thanks for all the help Lee! I guess due to my lack of server experience and that I only need to monitor a few labs here at the university I'll stick with webrick. On another note, I would not mind securing the page so no one can create new accounts. I'm guessing removing things from the webpage would be easiest for a newbie, I'm sure there is away to do this via the database but still a newbie. Again thanks, you were a great help!
Gerald

March 28, 2014
2:16 pm
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Hi Gerald,

You can remove the signup page by deleting everything on this page: https://github.com/other/thingspeak/blob/master/app/views/devise/registrations/new.html.erb .

lee

March 31, 2014
10:52 am
Avatar
gerald

Gold
Members
Forum Posts: 20
Member Since:
November 6, 2013
sp_UserOfflineSmall Offline

Thanks, I will look into that. Very last question, is there a folder on the server where I can retrieve feed data to make backups?

Gerald

March 31, 2014
11:23 am
Avatar
lee

Admin
Forum Posts: 457
Member Since:
March 14, 2011
sp_UserOfflineSmall Offline

Since all of the feed data is saved only in the database, there is no folder that allows you to retrieve feed data.

lee

April 14, 2014
1:57 am
Avatar
simonvo
New Member
Members
Forum Posts: 4
Member Since:
April 6, 2014
sp_UserOfflineSmall Offline

Hi Admin,
http://thingspeak.easytherm.co.uk/
I have problem with Nginx + Phusion Passenger + ThingSpeak.
Even i have done all the dependencies lib.
Thanks
Simon

Forum Timezone: America/New_York

Most Users Ever Online: 166

Currently Online:
20 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 272

Vinod: 240

piajola: 95

turgo: 70

vespapierre: 63

Adarsh_Murthy: 62

Member Stats:

Guest Posters: 1

Members: 8665

Moderators: 1

Admins: 2

Forum Stats:

Groups: 3

Forums: 14

Topics: 1600

Posts: 5760

Newest Members:

swatiiot, kalyanigaware16@gmail.com, Romi, Drakan01, majidkhan7, atharvganla

Moderators: cstapels: 460

Administrators: Hans: 405, lee: 457