Proper way to include data with an HTTP PATCH request

前端 未结 3 1572
无人共我
无人共我 2021-02-08 10:08

When I\'m putting together an HTTP PATCH request, what are my options to include data outside of URL parameters?

Will any of the following work, and what\'s the most com

3条回答
  •  甜味超标
    2021-02-08 10:35

    As rdlowrey writes, RFC 5789 does not mandate specific content types, so the choice of format is up to you.

    However, using the general formats you listed or making up your own format is not interoperable, and developers could have a hard time figuring out the semantics you chose. An official erratum to the RFC states this in a more formal way:

    The means of applying a PATCH request to a resource's state is determined by the request's media type. If a server receives a PATCH request with a media type whose specification does not define semantics specific to PATCH, the server SHOULD reject the request by returning the 415 Unsupported Media Type status code, unless a more specific error status code takes priority.

    In particular, servers SHOULD NOT assume PATCH semantics for generic media types that don't define them, such as application/xml or application/json. Doing so will cause interoperability issues, because the semantics of PATCH become specific to that resource, rather than general.

    (Quote formatted for readability, but unchanged otherwise)

    One media type whose specification defines PATCH semantics is application/json-patch+json, also called JSON Patch: RFC 6902. I suppose it could be considered the "standard" choice (at least) when dealing with data originally posted as JSON.

提交回复
热议问题