使用201创建重定向 [英] Redirecting with a 201 created
问题描述
有没有办法重定向201回答?
Is there a way to redirect through a 201 answer?
RFC指定必须在 Location <中指定新创建的资源/ code>标题,我确实指定了它。我假设浏览器会重定向,但它没有,即使页面没有内容。
The RFC specifies that the newly created resource must be specified in the Location
header, and I do specify it. I assumed that the browser would redirect but it doesn't, even if the page has no content.
我希望用户在POST操作后被重定向到新资源。因此我很想使用 303见其他
但201似乎更合适。
I want the user, after the POST action, to get redirected to the new resource. I'm therefore tempted to use 303 See Other
but a 201 seems more appropriate.
所以,有没有在没有用户干预且不依赖Javascript的情况下自动重定向流行浏览器的方法?
So, is there any way to automatically redirect popular browsers without user intervention and without relying on Javascript?
推荐答案
我认为你混淆了两种不同的语义响应 - 一个是告诉客户您已成功创建资源,以及它在哪里。客户端是否去获取它是一个不同的故事。
I think you're confusing two different semantic responses - one is telling the client that you successfully created a resource, and where it is. Whether the client goes to fetch it or not is a different story.
第二个是告诉客户端它已经为它请求的资源发送了错误的位置URI - 以及它应该再次尝试,但使用不同的URI。
The second is telling the client that it has sent the wrong location URI for a resource it's requesting - and that it should try again, but with a different URI.
在这种情况下,303是合适的 - 事实上,明确建议这样做:
A 303 is appropriate in this case - in fact, it's explicitly recommended for this:
(来自 rfc )
它主要用于允许POST操作的输出将用户代理重定向到选定的资源,因为这样做会以一种形式提供与POST响应相对应的信息。可以独立于原始请求单独标识,添加书签和缓存。
It is primarily used to allow the output of a POST action to redirect the user agent to a selected resource, since doing so provides the information corresponding to the POST response in a form that can be separately identified, bookmarked, and cached, independent of the original request.
这篇关于使用201创建重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!