PUT请求中是否需要body?

huangapple go评论55阅读模式
英文:

Is body required in PUT request?

问题

以下是翻译好的部分:

My thought is to use PUT without body, with this URLs:
我打算使用不带请求正文的PUT方法,并使用以下URL:

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

Is it possible to use PUT without a body?
是否可以在不带请求正文的情况下使用PUT方法?
Do you have any other suggestions?
您有其他建议吗?

英文:

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?

答案1

得分: 0

短回答:不。

在个人经验中,当仅使用幂等操作更新资源时,例如切换启用/禁用时,我也曾使用空的PUT请求。

可能重复:https://stackoverflow.com/questions/1233372/is-an-http-put-request-required-to-include-a-body

英文:

Short answer: No.

Personally, I have also used empty PUT requests when merely updating a resource using an idempotent action - like toggling enable/disable.

Possible duplicate of: https://stackoverflow.com/questions/1233372/is-an-http-put-request-required-to-include-a-body

答案2

得分: 0

> PUT 请求中是否需要 body?

不需要 - 使用空 body 的 PUT 请求是告诉 web 服务器我们希望资源的表示为零字节长。

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

> 我想设计一个 REST 端点来启用或禁用用户

你可能考虑一下之前的一些经验,比如 GitHub 的星标 API,它使用空 body 的 PUT 请求来点亮星标,使用 DELETE 请求来取消星标。

注意这两个请求使用相同的 URI - 这是合适的,因为我们希望通用的 缓存失效 能够“正常工作”。


在你的思维中搞清楚消息的语义(“请在 web 服务器上保存这个空文档”)和业务副作用(“启用用户”)之间的区别可能会有帮助。参见 Webber 2011

英文:

> 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.

huangapple
  • 本文由 发表于 2023年2月8日 18:48:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定