Opened 4 months ago

Closed 2 months ago

#35631 closed New feature (fixed)

Include quality value when parsing Accept header

Reported by: Jake Howard Owned by: Jake Howard
Component: HTTP handling Version: 5.0
Severity: Normal Keywords:
Cc: Jake Howard Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Requests have an accepts method for identifying the mimetype the user can receive. However, when multiple formats are provided, a quality value can be added to show the preferences on format. Handling this value currently needs to be done by hand.

I propose:

  1. Documenting the existence of Request.accepted_types
  2. Extract the quality value, and store it as MediaType.quality
  3. Sort Request.accepted_types by quality value

It would be nice to have an API to answer "I can return these mime types, which would you prefer?", which I guess could be handled manually for now.

Change History (12)

comment:1 by Carlton Gibson, 4 months ago

Triage Stage: UnreviewedAccepted

I think step 3 should read "Sort Request.accepted_types by quality value and specificity", e.g. text/*;q=0.8,text/html;q=0.8 should be sorted text/html first.

This is probably a sensible proposal. I suspect most people are not sending the quality value, so 1 is assumed and it has no effect. Nonetheless why not allow it?

We'd need a new helper API on HttpRequest taking a set of mime types to match against, since the specificity question (text/*) makes it slightly more complex than just returning the first match in accepted_types from the set.

DRF has implementations of this logic that can be used for reference:

comment:2 by Jake Howard, 4 months ago

Has patch: set

I've implemented the core of this feature here: PR, sans docs.

I'd appreciate a review, particularly around what API surface to expose.

Version 0, edited 4 months ago by Jake Howard (next)

comment:3 by Jake Howard, 4 months ago

PR now contains docs - review away!

comment:4 by Sarah Boyce, 3 months ago

Patch needs improvement: set

comment:5 by Jake Howard, 3 months ago

Patch needs improvement: unset

comment:6 by Sarah Boyce, 3 months ago

Patch needs improvement: set

comment:7 by Jake Howard, 3 months ago

Patch needs improvement: unset

comment:8 by Sarah Boyce, 3 months ago

Patch needs improvement: set

comment:9 by Jake Howard, 3 months ago

Patch needs improvement: unset

comment:10 by Sarah Boyce, 3 months ago

Patch needs improvement: set

comment:11 by Sarah Boyce, 2 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:12 by Sarah Boyce <42296566+sarahboyce@…>, 2 months ago

Resolution: fixed
Status: assignedclosed

In e161bd46:

Fixed #35631 -- Added HttpRequest.get_preferred_type().

Note: See TracTickets for help on using tickets.
Back to Top