0

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 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?(Just in high level...doesn't matter you can guide me in any language,Java,ruby,C,NodeJS)

Here is my code.Thank you in advance.

#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();
       }
      }
1

You should forget about MIME, that is only needed if you are sending multiple files to the server. Your best bet is to just send these headers, then a blank line, then your data:

POST /api/users HTTP/1.1
Accept: */*
Content-Type: application/octet-stream
Content-Length: 123

On the other end, you just look at "request.body" and it should have your binary data. (Replace 123 with sizeof(buffer))

  • Thank you BraveNewCurrency. If i send a jpeg bytes though application/octet-stream, on the other hand how hard is it to handle those bytes back to the image(what do i need to do)?Thank you again. – sparks May 19 '16 at 8:42
  • Er, you don't need to "do" anything, bytes are bytes. If you prefer, you can set Content-Type to be image/jpg. I suppose it could matter if your web server is checking content-types. (I assumed you were sending to a custom server, so you were not checking the content-type.) – BraveNewCurrency May 19 '16 at 20:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.