0

I am using the Arduino WifiWebClient to insert records into a RoR (Ruby on Rails) app. It works but it only inserts one field regardless of the number of arguments I send.

For example, if I test with Curl

curl -d "sale[itemId]=1234&sale[machineId]=4567"  http://myapp.herokuapp.com/sales

It will create a Sale record and populate both fields itemId and machineId. Everything OK.

But when I send the following instruction from the Arduino, it will only populate the first field:

(I am only including the POST method to simplifly the code, the rest of the code is similar to http://arduino.cc/en/Reference/WiFiClient)

void sendPOST() //client function to send/receive POST request data.
{
if (client.connect(URL,PORT)) {  //starts client connection, checks for connection
Serial.println("connected");

Serial.println("POST /sales HTTP/1.1"); //download text
Serial.println("From: Arduino1");
Serial.println("Host: evening-sierra-3769.herokuapp.com");
Serial.println("User-Agent: HTTPTool/1.1");
Serial.println("Connection: close");
Serial.println("Cache-Control : no-cache");
Serial.println("Pragma: no-cache");
Serial.println("Expires: -1");
Serial.println("Content-Type: application/x-www-form-urlencoded");
Serial.print("Content-Length: ");
Serial.println(31);
Serial.println();
Serial.println("sale[machineId]=\"09876543211234\"");
Serial.println("sale[itemId]=\"09876543211234\"");
Serial.println();

client.println("POST /sales HTTP/1.1"); //download text
client.println("From: Arduino1");
client.println("Host: evening-sierra-3769.herokuapp.com");  // Will be needed if apache is configured for VHOSTS
client.println("User-Agent: HTTPTool/1.1");
client.println("Connection: close");
client.println("Cache-Control : no-cache");
client.println("Pragma: no-cache");
client.println("Expires: -1");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(31);
client.println();
client.println("sale[machineId]=09876543211234\r\n");
client.println("sale[itemId]=09876543211234\r\n");
client.println();

Serial.println("ARDUINO: HTTP message sent");
delay(3000);
if(client.available())
{
  Serial.println("ARDUINO: HTTP message received");
  Serial.println("ARDUINO: printing received headers and script response...\n");

  while(client.available())
  {
    char c = client.read();
    Serial.print(c);
  }
}
else
{
  Serial.println("ARDUINO: no response received / no response received in time");
}

client.stop();
}
1
  • 31 is not the right content-length for the whole content you try to post. It only includes the first line.
    – jfpoilpret
    Oct 12 '14 at 6:11
1

Solved!

Changed the following lines:

client.println("Content-Type: multipart/form-data");   
client.print("Content-Length: ");
client.println(63);
client.println();
client.println("sale[itemId]=123456789012341234&sale[machineId]=123456789012341234\r\n");
client.println(); 

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.