3

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.

gnicholas
  • 2,041
  • 1
  • 21
  • 32
  • 2
    You have several problems. FIRST -- edit you sample and remove the vulgararity before you are reported to Stackoverflow. Second, you should fix your error message. It includes your own run off statements. Finally, you are not following the standards of how to generate chunk encoded transfers. (https://en.wikipedia.org/wiki/Chunked_transfer_encoding). The length of the chunk should be in hexadecimal. The number '25' does not match any of these correctly. – user590028 Jan 18 '15 at 17:46
  • The 1st line is 36 characters long (excluding CRLF). decimal 36 is hex 24 (and not 25). For the second line, your are missing the CRLF after the chunk size. – alk Jan 18 '15 at 17:53
  • The length of the chunks *are* in hex. Also I used client.println for the CRLF for the second line, but I'll switch it to being an explicit \r\n. I am very confused what is the proper method to do chunked transfer encoding as I could not get it to work. And I edited the post with the small wording changes just now – gnicholas Jan 18 '15 at 17:58
  • Also: just made the changes and am getting a (similar) error HTTP/1.1 400 Bad Request Content-Type: text/plain Connection: close Server: thin – gnicholas Jan 18 '15 at 18:05
  • 4
    The problem is not your code (I tested your HTTP request). But you didn't say what server you are using. Please note that IIS and Apache seems to have problems passing chunked requests to any runtime running as CGI/FastCGI module, and other servers (like nginx) only handle them properly with additional modules. For your HTTP request if I use Apache with mod_php, it works. If I use Apache with PHP under FastCGI it fails (PHP only sees an empty request body). – MV. Sep 03 '15 at 05:13

0 Answers0