How to Send binary data to HTTP server
Posted: Wed May 18, 2016 7:29 pm
I am new to low level programming.I am trying to send binary data to the http server.At the moment i just fill up the byte array with numbers and send them to the server(eventually i want to send image file). I can see the request in the server log as
Questions
1.My question is as you can see in the log, what is bytes=221 i thought if i sent byte array of length 32 i should see 32 bytes(or around 40s).And i made the buffer of length 1024 byte array, still in the server log is bytes=221.Are my bytes are even being sent?
2.How would i handle the bytes received on the server side just to be sure client.write actually succeeded?
Code: Select all
2016-05-18T23:30:26.688040+00:00 heroku[router]: at=info method=POST path="/api/users" host=myapp.herokuapp.com request_id=ed2b7852-7021-4642-a663-f9709e1aea70 fwd="99.89.114.63" dyno=web.1 connect=0ms service=2ms status=404 bytes=221
Questions
1.My question is as you can see in the log, what is bytes=221 i thought if i sent byte array of length 32 i should see 32 bytes(or around 40s).And i made the buffer of length 1024 byte array, still in the server log is bytes=221.Are my bytes are even being sent?
2.How would i handle the bytes received on the server side just to be sure client.write actually succeeded?
Code: Select all
#include <ESP8266WiFi.h>
#define SSID "ssid"
#define PASS_SSID "password"
#define HOST "myapp.herokuapp.com"
WiFiClient client;
byte buffer[32];
int bufOffset = 0;
byte startByte = 0;
byte nextByte = 0;
void setup() {
Serial.begin(115200);
checkConnection();
}
void loop() {
if (client.connect(HOST,80)) {
Serial.begin(115200);
Serial.println("conected to the server");
String start_request = "";
String end_request = "";
start_request = start_request + "--AaB03x" + "\n"
+ "Content-Disposition: form-data; name=\"picture\"; filename=\"cam.jpg\"" + "\n"
+ "Content-Type: image/jpeg" + "\n" + "Content-Transfer-Encoding: binary";
end_request = end_request + "\n" + "--AaB03x--" + "\n";
client.println("POST /api/users HTTP/1.1");
client.println("Accept: */*");
client.print("Content-Type: multipart/form-data; boundary=AaB03x");
client.println(start_request);
//Fill the buffer with values
nextByte = startByte++;
for(int ii = 0; ii < sizeof(buffer); ii++) {
buffer[ii] = nextByte++;
}
//write it to the server
int count = client.write(&buffer[bufOffset], sizeof(buffer));
//end the request
client.println(end_request);
client.stop();
}
}