Search code examples
restjerseymedia-type

How to upload binary file(Photos etc ) with Meta Data using REST


One way of doing is to convert the binary data to Base64 and send it along with meta data as MediaType.APPLICATION_JSON.

The problem with this is client has to convert the binary data into Base64 before sending.

I also tried sending using MediaType.APPLICATION_OCTET_STREAM format through REST. This works fine for binary data only.

One option is to send meta data in headers while using MediaType.APPLICATION_OCTET_STREAM.

Is there any better way?

Thanks


Solution

  • Use multipart/form-data. This is what it's meant for. Not sure which Jersey version you are using, but here is the link for the Jersey 2.x documentation for Multipart support. Here's for 1.x (not really much information). You will need to do some searching for using multipart with Javascript clients (there is a bunch of information out there)

    • Here is a good example of using Jersey 2.x with both the server side and the client API.
    • Here is an example with Jersey 1.x. You can see the API it not much different.