开发者

How would you pass HTTP Headers using a standard anchor tag?

开发者 https://www.devze.com 2023-04-09 03:50 出处:网络
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.

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 c开发者_StackOverflow社区an 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...


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"
}


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号