-->
Page 1 of 1

How to Send binary data to HTTP server

PostPosted: Wed May 18, 2016 7:29 pm
by sparkz19
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
Code: Select all2016-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();
       }
      }

Re: How to Send binary data to HTTP server

PostPosted: Thu May 19, 2016 7:40 am
by martinayotte
In the server log you've provided, it is mentioned "status=404", which is "page not found", so you request has not been processed. This means that the request has something wrong in it.

BTW, if you wish to send only 32 bytes of data, maybe you are over complexifying the task by using multipart form, while you could simply post the data by encoding it in base64 before sending it as a plain text field.

Re: How to Send binary data to HTTP server

PostPosted: Thu May 19, 2016 9:57 pm
by sparkz19
Status =404 is because i was not properly handling my action/route right, I already fixed that part.The reason i am using multipart form is because eventually i am expecting to send a jpeg file and probably even add another field of data. 32 bytes is just for testing purposes.So basically since i don't have the actual file to send at the moment, i just want to send those 32 bytes to pretend like its image binaries.But the problem now seems to be my http request from esp8266 is not formated right because when i use curl to upload file it works just fine,i am not sure what i am doing wrong.Another strange behavior when i send the request without `Content-length`
Code: Select allclient.write(buffer,buffer-size)
returns number of bytes written.And when i include content length
Code: Select allstart-request.length + end-request.length + buffer-size
client.write returns 0. What am i doing wrong? Thank you in advance.

Re: How to Send binary data to HTTP server

PostPosted: Fri May 20, 2016 1:49 pm
by martinayotte
For the requests, the one from ESP and the one from curl test, did you compared them to make sure they are identical ?

For the "client.write(buffer,buffer-size)", I'm not following your logic, the second argument should be the length and you provide a pointer subtracted by size.