Search code examples
resthttppostputpayload

Payload for PUT vs POST in REST API


I was asked below question in the Interview -

If you are hitting the same request with PUT and with POST http method then what will be the difference in payload?

Considering I don't have much experience working in REST, I didn't even know what payload is. I tried googling but didn't find anything conviencing.

Can anyone help?


Solution

  • according to this page restfulapi.net the PUT request should refer to already existing item (for example in a database connected). In other words, it is meant to update existing item. So the payload don't have to contain all the attributes of the item, just those you want to update.

    On the other hand POST is meant to insert new item. This means that the payload should contain (almost) everything.

    Thing is, if you send more same PUT requests, the item should remain equivalent to the situation with just one PUT send.

    If you send two same POST requests then two new same items (with different ids) will be created. This means that POST requests are not idempotent.

    Edit: here might be help too.