Chunked HTTP POST request:

gnicholas picture gnicholas · Jan 18, 2015 · Viewed 10k times · Source

This is what my chunked POST request looks like. I am trying to send chunks of data from an SD card on the arduino to a server through a chunked POST request. Need chunked as it will allow me to send the data which will not all fit into the arduino's memory at once.

 POST /upload HTTP/1.1 
 User-Agent: Arduino 
 Accept: */*
 Transfer-Encoding: chunked

 25 
 this is the text, of this file, wooo! 
 1d more test, of this file,luck 
 0

However I am getting an error when the server tries parsing the request: I am running a thin server with sinatra on it. Under the POST /upload route I put p params (the params hash in which POST request data is stored) and this is the output.

{"25\r\nthis is the text, of this file, wooo!\r\n"=>nil} Invalid request: Invalid HTTP format, parsing fails.

Any advice would be appreciated thanks!

Here is the C code I wrote. I'm using the Arduino CC3000 client library for my connection to the server.

String header = "POST /upload HTTP/1.1\r\n";
header += "User-Agent: Arduino\r\n";
header += "Accept \*/\*\r\n";
header += "Transfer-Encoding: chunked\r\n\r\n";

char hbuf[header.length() +1];
header.toCharArray(hbuf, header.length() +1);

String testfile = "this is the text, of this file, woo!\r\n";

char testbuf[testfile.length() +1];
testfile.toCharArray(testbuf, testfile.length() +1);

String testagain = "more test, of this file, luck\r\n";

char againbuf[testagain.length() +1];
testagain.toCharArray(againbuf, testagain.length() +1);

String lenone = String(testfile.length(), HEX);
lenone += "\r\n";
String lentwo = String(testagain.length(), HEX);
lentwo += "\r\n";

char buflenone[lenone.length() +1];
lenone.toCharArray(buflenone, lenone.length() +1);
char buflentwo[lentwo.length() +1];
lentwo.toCharArray(buflentwo, lentwo.length() +1);

Serial.println(buflenone);
Serial.println(buflentwo);

client.fastrprint(hbuf);
client.fastrprint(buflenone);
client.fastrprint(testbuf);
client.fastrprint(buflentwo);
client.fastrprint(againbuf);
client.fastrprint("0\r\n");

I've spent hours and hours and hours trying to figure out the proper way to send a chunked POST request but no resource seems to be useful(and consistent). Any help at all (besides pointing me to the HTTP RFC documentation which I tried looking at and is extremely cryptic) would be helpful I've been stuck for days now.

Answer