我可以更改浏览器发送的 HTTP 请求的标头吗?

发布于 2024-07-10 16:02:19 字数 564 浏览 6 评论 0原文

我正在研究一种宁静的设计,并希望尽可能多地使用 HTTP 方法(POSTGET...)和 HTTP 标头。 我已经发现浏览器不支持 HTTP 方法 PUTDELETE

现在,我希望获得同一资源的不同表示形式,并希望通过更改请求的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

﹎☆浅夏丿初晴 2024-07-17 16:02:19

我部分不同意 Milan 关于将请求的表示嵌入到 URI 中的建议。

如果可能的话,URI 应用于寻址资源,而用于隧道HTTP 方法/动词。 最终,如果单独创建 (POST) 或更新 (PUT) 无法达到目的,则可以将特定业务操作(编辑、锁定等)嵌入到 URI 中:

POST http://shonzilla.com/orders/08/165;edit

在请求 URI 中的特定表示的情况下,您需要破坏您的 URI 设计,最终使其变得更加丑陋,在同一个地方(即 URI)混合两个不同的 REST 概念,并使服务器端的请求处理变得更加困难。 米兰的建议以及许多人正在做的事情,包括。 Flickr,正是这样。

相反,更 RESTful 的方法是通过使用 Accept HTTP 标头来使用单独的位置对首选表示进行编码,该标头用于内容协商,其中客户端告诉服务器哪些内容类型它可以处理/处理并且服务器尝试满足客户端的请求。 此方法是 HTTP 1.1 标准的一部分,软件兼容并由网络浏览器也是如此。

比较一下:

GET /orders/08/165.xml HTTP/1.1
or
GET /orders/08/165&format=xml HTTP/1.1

GET /orders/08/165 HTTP/1.1
Accept: application/xml

Web 浏览器中,您可以使用 XMLHttpRequest 对象的 setRequestHeader 方法请求任何内容类型。 例如:

function getOrder(year, yearlyOrderId, contentType) {
 var client = new XMLHttpRequest();
 client.open("GET", "/order/" + year + "/" + yearlyOrderId);
 client.setRequestHeader("Accept", contentType);
 client.send(orderDetails);
}

总而言之:地址(即资源的 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:

POST http://shonzilla.com/orders/08/165;edit

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:

GET /orders/08/165.xml HTTP/1.1
or
GET /orders/08/165&format=xml HTTP/1.1

to this:

GET /orders/08/165 HTTP/1.1
Accept: application/xml

From a web browser you can request any content type by using setRequestHeader method of XMLHttpRequest object. For example:

function getOrder(year, yearlyOrderId, contentType) {
 var client = new XMLHttpRequest();
 client.open("GET", "/order/" + year + "/" + yearlyOrderId);
 client.setRequestHeader("Accept", contentType);
 client.send(orderDetails);
}

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 the Accept HTTP header.

Cheers!
Shonzilla

一念一轮回 2024-07-17 16:02:19

我想做完全相同的事情(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/

轮廓§ 2024-07-17 16:02:19

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.

初心未许 2024-07-17 16:02:19

我认为不可能按照您尝试的方式做到这一点。

通常通过向资源名称添加扩展名来指示可接受的数据格式。 因此,如果您有类似的资源

/resources/resource

,并且 GET /resources/resource 返回其 HTML 表示形式,以表明您需要其 XML 表示形式,则可以使用以下模式:

/resources/resource.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

/resources/resource

and GET /resources/resource returns its HTML representation, to indicate that you want its XML representation instead, you can use following pattern:

/resources/resource.xml

You have to do the accepted content type determination magic on the server side, then.

Or use Javascript as James suggests.

貪欢 2024-07-17 16:02:19

可能的解决方案

正如 Shonzilla 的回答

  1. /11565176">Milan 您可以通过更改请求的 Accept 标头中的值
  2. 明确提及文件格式来 在 URI 中(作为扩展名或在查询参数中

在浏览器中强制执行这些解决方案

但是尝试通过更改浏览器配置来实现此目的并不是一个灵活的解决方案。 并且您只能覆盖方法 1 的默认行为。
我不太清楚如何通过配置来实现第二种情况。

有些扩展可以让您添加标头,有些扩展可以让您修改 URL。 但是 Requestly 可让您通过很大的灵活性

PS:我是Requestly的维护者之一https://github.com/requestly/requestly


添加Accept 标头

创建一个标头规则,将 Accept 请求标头覆盖为您所需的值。

标题规则对于 Accept header override

如果您想对所有请求执行此操作,请使用通配符 * 或者您可以指定特定的匹配参数,例如此

URI 修改

对于 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

  1. changing the values in the Accept header of the request or
  2. explicitly mentioning the file format in the URI (as an extension or in the query param)

Enforcing 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

PS: I am one of the maintainers of Requestly https://github.com/requestly/requestly

Adding Accept Header

Create a header rule that overrides the Accept request header to your desired value.

header rule for Accept header override

If you want to do this for all your request use the wildcard * or you can specify specific matching parameters like this

URI modification

For the URI modification approach, either create a simple redirect for a specific request

simple redirect rule

or use a complex regex match like this to apply the rule for the specific resource types that you want

complex redirect to change URI extension

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

query param add format

今天小雨转甜 2024-07-17 16:02:19

使用一些 JavaScript!

xmlhttp=new XMLHttpRequest();
xmlhttp.open('PUT',http://www.mydomain.org/documents/standards/browsers/supportlist)
xmlhttp.send("page content goes here");

Use some javascript!

xmlhttp=new XMLHttpRequest();
xmlhttp.open('PUT',http://www.mydomain.org/documents/standards/browsers/supportlist)
xmlhttp.send("page content goes here");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文