Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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...

like image 293
exhuma Avatar asked Sep 29 '11 17:09

exhuma


1 Answers

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"
}
like image 84
Daff Avatar answered Sep 30 '22 07:09

Daff