Can I PUT without an ID?

前端 未结 3 1307
难免孤独
难免孤独 2020-12-11 04:48

I\'m designing an API Rest service that allows the user to upload a file to the server.

I\'m thinking this is a PUT request and it would go to server/resource/ID and

3条回答
  •  温柔的废话
    2020-12-11 05:32

    Too late for you, but I was with this same question and found a lot of wrong information, so I will let here what a found.

    There are 2 RFC that rules RESTful, the one about this question is RFC 7231, in that you will find:

    The PUT method requests that the state of the target resource be created or replaced with the state defined by the representation enclosed in the request message payload.

    So you can't send a PUT without the ID.

    A lot of RESTful API send a POST even on updates, that's wrong too by that same RFC, so you should always send the ID to create it with PUT, or should use POST to create and PUT to update, but remember that POST should always create, in another words, you will duplicate your file if you don't look for it first with GET.

    For more information: https://tools.ietf.org/html/rfc7231#section-4.3.3

提交回复
热议问题