0

I have a Arduino Mega with Wiznet 5100 Ethernet shield. Am trying to post some data onto localhost for started later on which will be accepted by a website. But when I try to post the data I get some error. When I try to send the same data via postman it gets accepted . But when it goes from arduino error occurs. The arduino Code is as follows:

client.println("POST /api/trackers/5a28eddaf57a6e2060e85601/solars/5a3bb2adf57a6e1a71ca135b/arduino_data HTTP/1.0");

client.println("Host: 192.168.6.16");

client.println("Accept: application/vnd.mirasol.v1");
Serial.println("After Accept application vnd");

client.println("User-Agent: Arduino/1.0");
Serial.println("After User Agent Arduino");

client.println("Accept: application/json");
Serial.println("After Accept application json");

client.println("Content-Length: ");
Serial.println("After content length");

client.println(postdata.length());

client.println("Content-Type: application/json");
Serial.println("After content type application json");

client.println("Connection: close");
Serial.println("After connection close");

client.println();
Serial.println("After client println");

client.println(postdata);
Serial.println("After post data");
Serial.println(postdata);

The output in the serial monitor is as follows:

    connecting...
connected
After Accept application vnd
After User Agent Arduino
After Accept application json
After content length
After content type application json
After connection close
After client println
After post data
{"current":10.50, "voltage":10.50}
H
disconnecting.

And the error shown in localhost is as follows:

2017-12-27 15:21:57 +0530: HTTP parse error, malformed request (): #<Puma::HttpParserError: Invalid HTTP format, parsing fails.>

I have the application in Ruby on Rails with Mongoid as the DB.

FYI: New to coding..

1

Your problem most likely stems from this bit:

client.println("Content-Length: ");
Serial.println("After content length");

client.println(postdata.length());

You have an extraneous "ln" in there causing the post data length to be on a separate line from the Content-Length:

Content-Length:
23

You should change the first println to a print. Also the order could do with changing to keep things logically grouped:

client.print("Content-Length: ");
client.println(postdata.length());

Serial.println("After content length");

Which would result in:

Content-Length: 23
  • Well did that and the rails server responded... But now am getting Completed 204 No Content . I want to send the data from the body instead of the URL to keep the security good in a way. – Mihir Joshi Dec 27 '17 at 10:51
  • I would suggest, for debugging, duplicate all client prints to the serial so you can see precisely what is being sent to the server. – Majenko Dec 27 '17 at 10:56
  • That 204 was there since I had not given any view or response for when the request would come in... Just went into the function and added a json response to it and it worked like a charm ... Kudos to u for helping ... – Mihir Joshi Dec 27 '17 at 11:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.