0

I don't have much experience with networking and my Googling skills don't seem to get me any further than this.

I need to send a file to a server with "file" being the HTTP POST key. Here is what I have:

MultipartEntityBuilder mpEntity = MultipartEntityBuilder.create();  

   mpEntity.addBinaryBody("file", image);//set up the object to send

    HttpPut put = new HttpPut("http://address:port"); 

    put.setEntity(mpEntity.build());//put the object to be sent

    //try sending
    try {
        HttpResponse response = client.execute(put);

...

I'm getting a 404 error when I process the response using an InputStream. The server is up and running and works fine when I test it from the terminal.

Jonathan
  • 135
  • 1
  • 10
  • You are using HTTP PUT instead of POST. Try replacing `HttpPut` with `HttpPost`. – Gergely Kőrössy Feb 28 '16 at 00:07
  • @GergelyKőrössy Yeah, I thought it was weird the tutorial used a PUT instead of a POST. I'm now getting a "error with upload" error, I'm wondering if I have the key-file pair right. – Jonathan Feb 28 '16 at 00:13
  • What tutorial are you reading? – Gergely Kőrössy Feb 28 '16 at 00:13
  • @GergelyKőrössy Not a tutorial, a Stack over flow tread (http://stackoverflow.com/questions/24239923/android-upload-image-and-json-using-multipartentitybuilder) actually. – Jonathan Feb 28 '16 at 00:18
  • What's on your server side? Also, try adding your image as it's in the SO thread: `mpEntity.addBinaryBody("file", image, ContentType.create("image/jpeg"), "image_name.jpg");`. – Gergely Kőrössy Feb 28 '16 at 00:22
  • I think they have some python code on the server side. But it seems to work fine now. Setting the 'ContentType' and image name seems to make a difference. Thanks a lot! – Jonathan Feb 28 '16 at 00:28

1 Answers1

0

Add the content type and the name of the file to the binary body like this:

mpEntity.addBinaryBody("file", image, ContentType.create("image/jpeg"), "image_name.jpg");
Gergely Kőrössy
  • 5,620
  • 3
  • 28
  • 44