0

I want to send a PUT request to a rails server from Arduino.

this works in curl:

curl -X PUT -d "barrel[gallons]=99" 192.168.0.7:3000/barrels/1

The server gets the call and the gallons attribute is updated.

I don't know how to format this message in Arduino.

String request = "PUT /barrels/1.json?barrel[gallons]=99 HTTP/1.0";
send_request(request);
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
// Send request
if (client.connected()) {
  client.println(request);     
  Serial.println("Connected & Data sent");
} 

It does successfully connect to the client but the message has no effect. How should I format the request?

LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46
tuna
  • 105
  • 2
  • 10

2 Answers2

0

If it works with curl you want the Arduino to generate exactly the same message. The issue is of course how to find out how it should look like. In such cases I usually use a debug proxy to intercept the message. The most convenient debug proxy I know of is Fiddler2. However in your simple case a tool like netcat or ncat is sufficient. Another route would be to sniff the connection with wireshark.

Once you can see the traffic it is usually very simple to fix all this kind of issues.

Udo Klein
  • 6,784
  • 1
  • 36
  • 61
0

Note your PUT request in cURL will become a TCP message like following:

PUT /barrels/1 HTTP/1.1
User-Agent: curl/7.21.7 (i386-pc-win32) libcurl/7.21.7 OpenSSL/0.9.8r zlib/1.2.5
Host: 192.168.0.7:3000
Accept: */*
Content-Length: 18
Content-Type: application/x-www-form-urlencoded

barrel[gallons]=99

Using Adafruit_CC3000_Client, you are using a TCP client, and not a HTTP client.

Also

/barrels/1.json?barrel[gallons]=99

is more like a GET then a PUT request. I indeed would use a GET request because this way I wouldn't be forced to supply a message body. So, I would try:

String request = "GET /barrels/1?barrel[gallons]=99 HTTP/1.1";
//send_request(request); //Why here?
Adafruit_CC3000_Client client = cc3000.connectTCP(ip, port);
// Send request
if (client.connected()) {
  client.println(request);
  client.println(); //Empty line to terminate header required.
  Serial.println("Connected & Data sent");
} 
LS_ᴅᴇᴠ
  • 10,823
  • 1
  • 23
  • 46