I have an restful app that uses PUT to replace existing data. For example, there's a user management url https://myserver/users, and to replace user data, for example, to change the name and the salary of the user with id=123, I use https://myserver/users/123 with application/json encoded new user's data in the request's body, like:
Now I need to manage users pictures as well. I. e. to have the possibility to upload the user's photo from the client (ecmascript spa) and to change it (to store, replacing existing, if any) on the server. The question is: how to upload binary picture with the PUT method? I use java on the server side, either pure servlets or jax-rs, if it is important. But I'd like to understand the "right" and "correct" RESTful way to do that. I see following possibilities:
- To include base64 encoded data in the json body as a string. But picture could be quite large, so it's not a way to go.
- To use multipart/form-data. But neither my servlet container, nor jax-rs are able to parse such a requests automatically, allowing multipart form data auto parsing for POST method only.
- To upload picture with the POST using dedicated URL, then to use the stored picture's id in the PUT. But it does not looks like the correct RESTful way for me, and 2 requests would be required to implement that.
So, my question is more common - what is the most convenient way to handle such a case?