2

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:

{
    name: "John",
    salary: 123.45
}

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:

  1. 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.
  2. 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.
  3. 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?

3

I would just treat it as a separate resource, and use post e.g. POST /users/123/profile-picture. This also makes it easier to add the profile to a web page, if that's what you're doing. e.g:

<img src="/users/123/profile-picture">
6
  • You mean 3rd possible solution in my question. Well, I think it's probably the easiest way. But even in this case, if I change the picture, I still shall use PUT to upload a new one as I'm changing it. Or, delete previous one, and create new one.
    – BbIKTOP
    Sep 1 '20 at 19:06
  • also, it will make a client code a bit complicated...
    – BbIKTOP
    Sep 1 '20 at 19:08
  • 3
    I would also go with a different endpoint for uploading the image. If you pick an endpoint as @Blake suggest that reference the user there should be no confusion about where the image is attached and you shouldn't need an additional request. However this gives the challenge that you need to support both PUT and POST and the client needs to know which to use. I would probably go with just supporting POST and argue that a new image is not a modification but a new resource. Sep 1 '20 at 19:33
  • So, I assume, no "restful" way to implement that. Thank you!
    – BbIKTOP
    Sep 1 '20 at 19:54
  • Why do you think that this wouldn't be restful? Which of the REST guidelines do you feel is violated? Sep 2 '20 at 14:15

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.