Can the Location header be used for multiple resou

2019-02-16 12:21发布

What is the correct verb and response to accept a batch PUT create and then return multiple locations? The Location header only appears to support one single Uri.

I assumed originally I could use PUT for a batch create and return an string array with a list of Uris, but in looking at the specification, that doesn't appear to be supported, but it isn't entirely clear either.

Are multiple Location headers permissible as an alternative?

Any advice?

标签: http rest
3条回答
beautiful°
2楼-- · 2019-02-16 12:43

PUT can only create one resource, because according to the specification:

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.

(http://tools.ietf.org/html/rfc7231#section-4.3.4)

However, it is possible for a request with a different verb (such as POST) to create multiple resources and return a 201 Created status. According to the specification:

The primary resource created by the request is identified by either a Location header field in the response or, if no Location field is received, by the effective request URI. The 201 response payload typically describes and links to the resource(s) created.

(http://tools.ietf.org/html/rfc7231#section-6.3.2)

So it is OK to return the URIs of multiple created resources in a 201 response to, e.g., a POST request, but the Location header may only contain one URI.

查看更多
男人必须洒脱
3楼-- · 2019-02-16 12:44

No, you can have only one Location header field (and yes, it is clear from the spec).

That being said, PUT is for creating/updating a single resource. It seems that you're using it for something it is not designed for...

查看更多
啃猪蹄的小仙女
4楼-- · 2019-02-16 13:03

RFC 5988 introduces Link HTTP Header. It can be used for this purpose.

查看更多
登录 后发表回答