Http accept type

939 views
Skip to first unread message

bsr

unread,
May 20, 2011, 4:02:26 PM5/20/11
to golang-nuts
Hello,
How easily find a requested format type from an Http request. I
looked at the accept header and seems like I may extract this from
Accept field

v := r.Header.Get("Accept")
Println(v) ==> application/json, text/javascript, */*; q=0.01

Is it reliable/consistent to get the type if I get the substring
between first "/ " and "," , or a better way.

thanks.
bsr.

Gustavo Niemeyer

unread,
May 20, 2011, 4:09:50 PM5/20/11
to bsr, golang-nuts
> Is it reliable/consistent  to get the type if I get the substring
> between first "/ " and "," , or a better way.

You can find all details in the RFC:

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

--
Gustavo Niemeyer
http://niemeyer.net
http://niemeyer.net/blog
http://niemeyer.net/twitter

Brad Fitzpatrick

unread,
May 20, 2011, 4:12:53 PM5/20/11
to Gustavo Niemeyer, bsr, golang-nuts
Anybody want to write a method on *http.Request that returns this header parsed into a slice of sorted Go structs?

Would be fun, but I don't think Accept is used too often in practice, sadly.

Dmitry Chestnykh

unread,
May 20, 2011, 5:21:35 PM5/20/11
to golan...@googlegroups.com, Gustavo Niemeyer, bsr
On Friday, May 20, 2011 10:12:53 PM UTC+2, Brad Fitzpatrick wrote:
Anybody want to write a method on *http.Request that returns this header parsed into a slice of sorted Go structs?

I'd like to use something like this to parse Accept-Language too. Maybe add some generic method that returns a slice of strings?

-Dmitry

Brad Fitzpatrick

unread,
May 20, 2011, 5:37:38 PM5/20/11
to golang-nuts, Gustavo Niemeyer, bsr
Feel free to write one.  Accept-Language is used in the real world, at least.  It'd be useful off *http.Request.

William Waites

unread,
May 20, 2011, 5:56:21 PM5/20/11
to Brad Fitzpatrick, Gustavo Niemeyer, bsr, golang-nuts
* [2011-05-20 13:12:53 -0700] Brad Fitzpatrick <brad...@golang.org> �crit:

] Anybody want to write a method on *http.Request that returns this header


] parsed into a slice of sorted Go structs?
]
] Would be fun, but I don't think Accept is used too often in practice, sadly.

It's heavily used in semantic-web applications. I've actually
written a package for this, used in production, and sanity
checked by a member of the pedantic-web group whose hobby is
finding (extremely common) bugs in content-type negotiation.

https://bitbucket.org/ww/goautoneg

Contains two functions, ParseAccept which does what it says
on the tin, and Negotiate which choses the most appropriate
from a list of server-provided alternatives.

Cheers,
-w
--
William Waites <mailto:w...@styx.org>
http://river.styx.org/ww/ <sip:w...@styx.org>
F4B3 39BF E775 CF42 0BAB 3DF0 BE40 A6DF B06F FD45

William Waites

unread,
May 20, 2011, 6:02:58 PM5/20/11
to Brad Fitzpatrick, Gustavo Niemeyer, bsr, golang-nuts
An interesting side note, from one of the test cases:

application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5

That's what Chrome sends. Read it closely. Yes, given the choice,
Chrome prefers pictures of web pages (image/png; q=1.0) to old
style HTML (text/html; q=0.9).

I reported this bug some time ago but it doesn't seem to have
been fixed yet...

Brad Fitzpatrick

unread,
May 20, 2011, 6:05:27 PM5/20/11
to William Waites, Gustavo Niemeyer, bsr, golang-nuts
On Fri, May 20, 2011 at 3:02 PM, William Waites <w...@styx.org> wrote:
An interesting side note, from one of the test cases:

You actually only appear to have one TOTAL test case.  :-)

But that aside, this looks pretty nice.  You want to submit it for inclusion in the standard http library?

William Waites

unread,
May 20, 2011, 6:11:52 PM5/20/11
to Brad Fitzpatrick, Gustavo Niemeyer, bsr, golang-nuts
* [2011-05-20 15:05:27 -0700] Brad Fitzpatrick <brad...@golang.org> �crit:

] You actually only appear to have one TOTAL test case. :-)

hmmm... you're right... I recall others but maybe they were from
the python version that I had written some time before.

] But that aside, this looks pretty nice. You want to submit it for inclusion


] in the standard http library?

Happily. I'll see if I can figure out how to use the code
review (that is what I should do, right?)

Brad Fitzpatrick

unread,
May 20, 2011, 6:17:34 PM5/20/11
to William Waites, Gustavo Niemeyer, bsr, golang-nuts
On Fri, May 20, 2011 at 3:11 PM, William Waites <w...@styx.org> wrote:
* [2011-05-20 15:05:27 -0700] Brad Fitzpatrick <brad...@golang.org> écrit:

] You actually only appear to have one TOTAL test case.  :-)

hmmm... you're right... I recall others but maybe they were from
the python version that I had written some time before.

] But that aside, this looks pretty nice.  You want to submit it for inclusion
] in the standard http library?

Happily. I'll see if I can figure out how to use the code
review (that is what I should do, right?)

Yeah, I found it a pain to setup the first time (because I didn't know hg, mostly) but the docs are pretty good:


And once you've done that once, you'll have to amortize the pain by sending in more patches.  :-)

Reply all
Reply to author
Forward
0 new messages