如何使用标准锚标记传递 HTTP 标头?

发布于 2024-12-07 19:32:28 字数 513 浏览 1 评论 0原文

根据 HTML4 参考,没有属性可以使用锚标记传递 HTTP 标头。

我想提供一个使用 Accept 标头请求特定文件类型的链接。

我能看到的唯一方法就是顺其自然,并传递一个 GET 参数。

您可能会问我为什么要这样做...我打算将一堆方法公开为公共 API,并将结果作为 JSON 提供。当使用 JavaScript 或其他编程语言执行请求时,使用 Accept 标头来请求特定的响应格式是“正确的方法”。但这意味着我需要在代码中容纳 Accept 标头 GET 参数,这听起来像是逻辑的重复。

这个话题在很大程度上是有争议的,因为这样的链接可能无法在浏览器中添加书签...仍然...我想知道是否可以在没有太多魔法的情况下...

According to the HTML4 reference there's no attribute to pass on HTTP headers using the anchor tag.

I would like to offer a link requesting for a specific file type using the Accept header.

The only way I can see is simply let it be, and pass a GET parameter.

You may as why I would want to do this... I intend to expose a bunch of methods as a public API, serving the results as JSON. And when doing requests using JavaScript, or another programming language, using the Accept header to request a specific response format is "The Right Way" to do it. But that would mean that I need to accommodate both the Accept header and the GET parameter in my code, which smells like a duplication of logic.

This topic is largely debatable, as such links may not be possible to bookmark in the browser... still... I'd like to know if it was possible without too much magic...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

西瑶 2024-12-14 19:32:28

除了使用 GET 参数或类似的扩展之外,我没有看到其他方法

http://myurl/page?format=json

或更好

http://myurl/page.json

它覆盖了接受标头(因为浏览器只会发送它的默认标头
接受标头)。然后你只需要初始化一个格式来接受这样的标头映射(我根本没有找到重复的逻辑):

{
    "json" : "application/json",
    "html" : "text/html"
}

I don't see another way than using the GET parameter or an extension like

http://myurl/page?format=json

or better

http://myurl/page.json

Which overrides the accept header (since the browser will only send it's default
accept header). Then you just need to initialize a format to accept header mapping like this (which I don't find duplicate logic at all):

{
    "json" : "application/json",
    "html" : "text/html"
}
阿楠 2024-12-14 19:32:28

你不能。

我打算将一堆方法公开为公共 API,以 JSON 形式提供结果。当使用 JavaScript 或其他编程语言执行请求时,使用 Accept 标头来请求特定的响应格式是“正确的方法”。但这意味着我需要在代码中容纳 Accept 标头和 GET 参数,这听起来像是逻辑的重复。

如果我理解正确的话,你就不必这样做。浏览器已经提供了 Accept 标头。

You can't.

I intend to expose a bunch of methods as a public API, serving the results as JSON. And when doing requests using JavaScript, or another programming language, using the Accept header to request a specific response format is "The Right Way" to do it. But that would mean that I need to accommodate both the Accept header and the GET parameter in my code, which smells like a duplication of logic.

If I understand you correctly, you don't have to do this anyway. Browsers already supply an Accept header.

长亭外,古道边 2024-12-14 19:32:28

嗯,好像如果你的结果是 JSON,你无论如何都会从脚本发送/接收,它可以提供你想要的任何标头。只需让您的链接调用脚本函数即可完成。

Hmm, seems like if your results are JSON, you will be sending / receiving from script anyway, which can provide any header you want. Just have your link call a script function and you're done.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文