Search code examples
restpatchput

Is body required in PUT request?


I want to design my REST endpoint for enabling or disabling a user

My thought is to use PUT without body, with this URLs:

  1. PUT /.../users/{userName}/enable
  2. PUT /.../users/{userName}/disable

Is it possible to use PUT without a body? Do you have any other suggestions?


Solution

  • Is body required in PUT request?

    No - a PUT request with an empty body is how we would tell a web server that we want the representation of a resource to be zero bytes long.

    PUT /example HTTP/1.0
    Content-Type: text/plain
    Content-Length: 0
    
    

    I want to design my REST endpoint for enabling or disabling a user

    A bit of prior art that you might consider is the github starring API, which uses PUT with an empty body to turn on a star, and DELETE to turn off a star.

    Note that both requests use the same URI - that's appropriate because we want general purpose cache invalidation to "just work".


    It may be helpful to be clear in your mind the differences between the semantics of the message ("please save this empty document on the web server") and the business side effects ("enable the user"). See Webber 2011.