Parse Accept Header

Does anyone have any suggestions (or a regular expression) for parsing the HTTP Accept header?

I am trying to do some content-type negotiation in ASP.NET MVC. There doesn't seem to be a built in way (which is fine, because there are a lot of schools of thought here), but the parsing is not entirely trivial and I would rather not re-invent the wheel if someone has already done it well and is willing to share.

Have you seen this article? It gives a pretty comprehensive implementation for parsing the Accept header and subsequently doing something useful with it.

HTTP Accept-* headers parsing. accept is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components  HTTP::Accept. Provides a robust set of parsers for dealing with HTTP Accept, Accept-Language, Accept-Encoding, Accept-Charset headers. Motivation. I've been developing some tools for building RESTful endpoints and part of that involved versioning.

As of .NET 4.5 (I think—Microsoft have made info on framework versions < 4.5 rather obscure these days), you can use one of the the built in parsers from System.Net.Http.Headers:

public IOrderedEnumerable<MediaTypeWithQualityHeaderValue> GetMediaTypes(string headerValue) =>
        .OrderByDescending(mt => mt.Quality.GetValueOrDefault(1));

Then you can do something like this:

var headerValue = "application/json, text/javascript, */*; q=0.01";
var mediaTypes = GetMediaTypes(headerValue);

Giving you a nice list of all the media types, where the preferred option is the first item. Here's a LINQPad Dump of the mediaTypes result from the example:

Hat tip to this answer, for getting me on the right track.

PHP class to parse Accept HTTP header and sort accepted mime types according to the HTTP Specification (RFC2616). Star 11. Watch  The Accept request HTTP header advertises which content types, expressed as MIME types, the client is able to understand. Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Type response header.

I've written a parser in PHP. It's not complex, but it will give you an array of mime types in order of preference.

Let's start by looking at how to parse an Accept header correctly. All Accept headers have a comma-separated list of values, and some include a q value that​  The mere presence of the string "json" anywhere in the Accept header seems to be enough to cause the problem. The Accept header might be hard to read and parse, but they're not even trying. I'd love to know why this is becoming more and more common.

Found another implementation in php here

The Accept-Language request HTTP header advertises which languages the client is able to understand, and which locale variant is preferred. HTTP Accept-* headers parsing. accept is part of the hapi ecosystem and was designed to work seamlessly with the hapi web framework and its other components (but works great on its own or with other frameworks).

After reading the article I decided to not write a function for the Accept header myself ;)

Fortunately the article points to a good library: - in my case I need it as a Node.js module:

public static HttpLanguageRangeWithQualityHeaderValue Parse(string input); and quality factor information used in the Accept-Encoding HTTP header. The Accept-Language request-header field is similar to Accept, but restricts the set of natural languages that are preferred as a response to the request. Language tags are defined in section 3.10. Accept-Language = "Accept-Language" ":" 1# ( language-range [ ";" "q" "=" qvalue ] ) language-range = ( ( 1*8ALPHA * ( "-" 1*8ALPHA ) ) | "*" ) Each language-range MAY be given an associated quality value which represents an estimate of the user's preference for the languages specified by that

Converts a string to an StringWithQualityHeaderValue instance. public: static System::Net::Http::Headers::StringWithQualityHeaderValue ^ Parse(System::​String ^  To achieve that, you need to parse the Accept header and create routing rules that can use that information. I created a sandbox with a very simple API that shows how you can route requests based on a requested API version. Version 1 of this API just returns the static list of jobs.

ATTRIBUTES. string. The header string as passed to new . values. The given media types in the prioritized order. Header | Values. The HttpHeaders object has accessors for common application headers. When we access a header by name from a Map, MultiValueMap or the HttpHeaders object, we'll get a null if it isn't present.

Description. Parse the accept-language header from a HTTP request Description. Parse 'Accept-Language' header and cache the result in request. As you correctly note, the Accept header is used by HTTP clients to tell the server what content types they'll accept. The server will then send back a response, which will include a Content-Type header telling the client what the content type of the returned content actually is.

  • Good question - I'm looking for this in another project, as well!
  • the linked site is "disabled"