-2

While trying to upload an image using a rest client (Postman) I am getting the status as 201 created and a file is uploaded, but the images are broken in the website.

This is what I am trying in the Postman.

URL [POST] 
platform/memories/memories?title=testImage

Headers:
Content-Type : image/jpeg
Authorization : Bearer ACCESS_TOKEN

Body
Form-data -> Choose file

Are there any other headers do we have to add apart from content type and Access_token?

When I try as binary in Postman instead of form-data image upload properly.

In java code I am converting an image to byte[] and using ByteArrayRequestEntity uploading to the web, in that case also the images are broken.

Any thoughts?

n-verbitsky
  • 552
  • 2
  • 9
  • 20
Lenin
  • 492
  • 1
  • 8
  • 30
  • So what, specifically, is your question? We need more info. The fact that Postman shows a 201 response simply means that your web service responded with a 201. What is broken about your images? – mkasberg Sep 27 '15 at 04:15
  • yeah, getting 201 seems everything fine as it conveys image created in the website and that's the relevance of this question. The images seems to be corrupted, when clicking on the uploaded images, says processing error and it doesn't open. – Lenin Sep 27 '15 at 11:22
  • 2
    Download back the uploaded file, and make byte to byte comparison of base and uploaded images. (On windows machine you can use totalcommander file compare utility) What bytes are corrupted? – Vovka Sep 27 '15 at 16:44
  • are images "broken" in the same way when you upload them via postman and via your code? – ilj Sep 29 '15 at 04:43
  • @Vovka so as you said did the file comparison and this 3 lines are getting prefixed with my uploaded image ------WebKitFormBoundary5j0AJzLzqTtBwhMP Content-Disposition: form-data; name="test"; filename="14-900x562.png" Content-Type: image/png ‰PNG and once i remove these lines manually from the image file after open it in text editor,bingo.. the image is fine. So why these lines getting prefixed in my file? – Lenin Sep 29 '15 at 19:25
  • Have you tried The Ancient answer? replace Content-Type: "image/png" -> "multipart/form-data". As it looks like you send not only image data but form data also. – Vovka Sep 29 '15 at 19:58
  • No, only image i am sending. – Lenin Sep 30 '15 at 09:26
  • See my comment to my answer – The Ancient Oct 03 '15 at 10:38

1 Answers1

0

It seems you set the content type wrongly. In case you select image/jpeg, most probably you can use binary in the body. If you selected form-data in the body, I would expect multipart/form-data as a content type.

I do not see your server side code, so it is difficult to judge, but I assume, the endpoint reacts on the content type and expects the content correspondingly shaped.

The Ancient
  • 382
  • 3
  • 13
  • @Lenin, see. That is exactly what happens. You tell the receiver by setting content-type to "image/jpeg": - now binary data will come. But in fact when you select form-data as body in Postman, you build multipart: prepend contend disposition info and so on. Then the receiver, as it expects actually a blob, tries to interpret the entire body (including what Postman added) as plain content. That is, sees the part of form data as part of the image data. Result --> broken image. So set the content type properly, according to how you are going to serialize the content – The Ancient Sep 30 '15 at 10:56
  • see http://stackoverflow.com/questions/16015548/tool-for-sending-multipart-form-data-request Re PostMan corrupting the header - can you use a different client or html form to test? – Peter Scott Oct 03 '15 at 18:46
  • @PeterScott Thanks for your kind suggestion, i tried through java code and facing same issue. Contacted API people for further clarification. Waiting for their response. – Lenin Oct 05 '15 at 10:41