我可以更改浏览器发送的 HTTP 请求的标头吗?
我正在研究一种宁静的设计,并希望尽可能多地使用 HTTP 方法(POST
、GET
...)和 HTTP 标头。 我已经发现浏览器不支持 HTTP 方法 PUT
和 DELETE
。
现在,我希望获得同一资源的不同表示形式,并希望通过更改请求的 Accept
标头来实现此目的。 根据此Accept标头,服务器可以为同一资源提供不同的视图。
问题是我没有找到一种方法来告诉我的浏览器更改此标头。
标签有一个 type 属性,可以有一个 mime 类型,看起来是一个不错的候选者,但标头仍然是浏览器默认值(在 Firefox 中,可以在 about:config
中更改它)使用 network.http.accept.default
键)。
I'm looking into a restful design and would like to use the HTTP methods (POST
, GET
, ...) and HTTP headers as much as possible. I already found out that the HTTP methods PUT
and DELETE
are not supported from the browser.
Now I'm looking to get different representations of the same resource and would like to do this by changing the Accept
header of the request. Depending on this Accept
header, the server can serve a different view on the same resource.
Problem is that I didn't find a way to tell my browser to change this header.
The <a..>
tag has a type attribute, that can have a mime type, looked like a good candidate but the header was still the browser default (in Firefox it can be changed in about:config
with the network.http.accept.default
key).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我部分不同意 Milan 关于将请求的表示嵌入到 URI 中的建议。
如果可能的话,URI 应仅用于寻址资源,而不用于隧道HTTP 方法/动词。 最终,如果单独创建 (POST) 或更新 (PUT) 无法达到目的,则可以将特定业务操作(编辑、锁定等)嵌入到 URI 中:
在请求 URI 中的特定表示的情况下,您需要破坏您的 URI 设计,最终使其变得更加丑陋,在同一个地方(即 URI)混合两个不同的 REST 概念,并使服务器端的请求处理变得更加困难。 米兰的建议以及许多人正在做的事情,包括。 Flickr,正是这样。
相反,更 RESTful 的方法是通过使用
Accept
HTTP 标头来使用单独的位置对首选表示进行编码,该标头用于内容协商,其中客户端告诉服务器哪些内容类型它可以处理/处理并且服务器尝试满足客户端的请求。 此方法是 HTTP 1.1 标准的一部分,软件兼容并由网络浏览器也是如此。比较一下:
从
Web 浏览器中,您可以使用
XMLHttpRequest
对象的setRequestHeader
方法请求任何内容类型。 例如:总而言之:地址(即资源的 URI)应该独立于其表示形式,并且
XMLHttpRequest.setRequestHeader
方法允许您使用Accept
HTTP 标头。干杯!
尚齐拉
I would partially disagree with Milan's suggestion of embedding the requested representation in the URI.
If anyhow possible, URIs should only be used for addressing resources and not for tunneling HTTP methods/verbs. Eventually, specific business action (edit, lock, etc.) could be embedded in the URI if create (POST) or update (PUT) alone do not serve the purpose:
In the case of requesting a particular representation in URI you would need to disrupt your URI design eventually making it uglier, mixing two distinct REST concepts in the same place (i.e. URI) and making it harder to generically process requests on the server-side. What Milan is suggesting and many are doing the same, incl. Flickr, is exactly this.
Instead, a more RESTful approach would be using a separate place to encode preferred representation by using
Accept
HTTP header which is used for content negotiation where client tells to the server which content types it can handle/process and server tries to fulfill client's request. This approach is a part of HTTP 1.1 standard, software compliant and supported by web browsers as well.Compare this:
to this:
From a web browser you can request any content type by using
setRequestHeader
method ofXMLHttpRequest
object. For example:To sum it up: the address, i.e. the URI of a resource should be independent of its representation and
XMLHttpRequest.setRequestHeader
method allows you to request any representation using theAccept
HTTP header.Cheers!
Shonzilla
我想做完全相同的事情(RESTful Web 服务),我偶然发现了这个 Firefox 插件,它可以让您修改请求的接受标头(实际上,任何请求标头)。 它工作完美。
https://addons.mozilla.org/en-US/firefox/addon/ 967/
I was looking to do exactly the same thing (RESTful web service), and I stumbled upon this firefox addon, which lets you modify the accept headers (actually, any request headers) for requests. It works perfectly.
https://addons.mozilla.org/en-US/firefox/addon/967/
Google Chrome 的 ModHeader 扩展程序也是一个不错的选择。 您只需设置所需的标头,然后在浏览器中输入 URL,当您点击 URL 时,它会自动从扩展程序中获取标头。 唯一的问题是,它会为您将访问的每个 URL 发送标头,因此您必须在使用后禁用或删除它。
ModHeader extension for Google Chrome, is also a good option. You can just set the Headers you want and just enter the URL in the browser, it will automatically take the headers from the extension when you hit the url. Only thing is, it will send headers for each and every URL you will hit so you have to disable or delete it after use.
我认为不可能按照您尝试的方式做到这一点。
通常通过向资源名称添加扩展名来指示可接受的数据格式。 因此,如果您有类似的资源
,并且
GET /resources/resource
返回其 HTML 表示形式,以表明您需要其 XML 表示形式,则可以使用以下模式:您必须确定接受的内容类型那么,服务器端的魔法。
或者按照 James 的建议使用 Javascript。
I don't think it's possible to do it in the way you are trying to do it.
Indication of the accepted data format is usually done through adding the extension to the resource name. So, if you have resource like
and
GET /resources/resource
returns its HTML representation, to indicate that you want its XML representation instead, you can use following pattern:You have to do the accepted content type determination magic on the server side, then.
Or use Javascript as James suggests.
可能的解决方案
正如 Shonzilla 的回答和
Accept
标头中的值或在浏览器中强制执行这些解决方案
但是尝试通过更改浏览器配置来实现此目的并不是一个灵活的解决方案。 并且您只能覆盖方法 1 的默认行为。
我不太清楚如何通过配置来实现第二种情况。
有些扩展可以让您添加标头,有些扩展可以让您修改 URL。 但是 Requestly 可让您通过很大的灵活性
添加Accept 标头
创建一个标头规则,将
Accept
请求标头覆盖为您所需的值。如果您想对所有请求执行此操作,请使用通配符
*
或者您可以指定特定的匹配参数,例如此URI 修改
对于 URI 修改方法,可以创建针对特定请求的简单重定向
或使用像这样的复杂正则表达式匹配来将规则应用于您想要的特定资源类型
如果服务器需要 mime 类型作为查询参数发送(这很大程度上取决于实现),例如,在关键
格式
中,您可以创建查询参数规则< /a>
Possible solutions
As mentioned in the answer from Shonzilla and Milan you can get the resource format that you want by either
Accept
header of the request orEnforcing these solutions in your browser
But trying to do this by changing the browser config is not a flexible solution. And you can only override the default behavior for method 1.
I am not really aware of how one would implement the second case via the config.
There are extensions that let you add headers and some that let you modify the URL. But Requestly lets you do all of this with a lot of flexibility
Adding Accept Header
Create a header rule that overrides the
Accept
request header to your desired value.If you want to do this for all your request use the wildcard
*
or you can specify specific matching parameters like thisURI modification
For the URI modification approach, either create a simple redirect for a specific request
or use a complex regex match like this to apply the rule for the specific resource types that you want
If the server requires mime type to be sent as a query param (this very much depends on the implmentation) for eg, in the key
format
, you can create a query param rule使用一些 JavaScript!
Use some javascript!