Skip to content

Fixed #37232 -- Added support for the HTTP QUERY method. - #21855

Merged
jacobtylerwalls merged 1 commit into
django:mainfrom
mbeijen:ticket_37232
Oct 26, 2026
Merged

jacobtylerwalls merged 1 commit into
django:mainfrom
mbeijen:ticket_37232

Conversation

@mbeijen

@mbeijen mbeijen commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor

Trac ticket number

ticket-37232

Branch description

Implements the HTTP QUERY method (RFC 10008) as a safe, idempotent method
throughout Django: exempt from CSRF protection, evaluated for request
preconditions as GET is, accepted by require_safe(), and dispatched by
View via a query() handler. Client.query() is added to the test client,
async client, and both request factories. Query content is exposed as
HttpRequest.body and is not parsed into HttpRequest.POST, per the
discussion in comment 5 on the ticket.

Caching is deliberately left for a follow-up. RFC 10008 Section 2.7 requires
the cache key to incorporate the request content, which the cache middleware
and cache_page() don't do, so they continue to ignore QUERY. This is stated
in the release notes so there are no silent incorrect cache hits.

Three points I'd like reviewer input on:

  1. require_safe now accepts QUERY. The ticket asks for this, but it means
    existing views gain a method they didn't have. Noted in the release notes
    with require_http_methods(["GET", "HEAD"]) as the opt-out.
  2. APPEND_SLASH: I added QUERY to the DEBUG-time RuntimeError rather than
    blocking the redirect outright. RFC 10008 Section 2.5 states the POST-to-GET
    exception does not apply to QUERY, so a compliant client re-sends the
    QUERY with its content and the redirect is spec-conformant.
  3. Client.query() defaults to content_type="application/octet-stream" to
    match put()/patch()/delete(), though RFC 10008 Section 2 requires a
    meaningful Content-Type. Happy to change the default.

Not addressed, and arguably a separate ticket: follow=True follows redirects
as GET for every method, so a 307/308 on QUERY is not replayed as QUERY.

AI Assistance Disclosure (REQUIRED)

  • If AI tools were used, I have disclosed which ones, and fully
    reviewed and verified their output.

Claude Code (Opus 5) was used to draft the implementation, tests, and docs.
I reviewed and verified the full diff, and ran the test suite and linters
locally.

Checklist

  • This PR follows the contribution guidelines.
  • This PR does not disclose a security vulnerability.
  • This PR targets the main branch.
  • The commit message is written in past tense, mentions the ticket
    number, and ends with a period.
  • I have not requested, and will not request, an automated AI review
    for this PR.
  • I have checked the "Has patch" ticket flag in the Trac system.
  • I have added or updated relevant tests.
  • I have added or updated relevant docs, including release notes.

@BenA-SA BenA-SA left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, I'd been looking at this ticket too, and had a patch of my own on the go built on Jonathan's reference implementation. Yours came out very close to mine.

Comparing the two, one thing differed and I think it might be an issue. I've noted it inline with a suggestion, but I'm not overly familiar with this part of the code so I might be wrong. Let me know if so.

Claude Code assisted with the investigation; I ran the tests and reviewed the diff myself.

Comment thread django/views/generic/base.py Resolved
Comment thread tests/generic_views/test_base.py Resolved
@jacobtylerwalls jacobtylerwalls added the coverage Generate a comment with a coverage diff label Sep 1, 2026

@jacobtylerwalls jacobtylerwalls left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks for working on this. Not a full review yet, but an early query inquiry for you:

  • doc updates for DATA_UPLOAD_MAX_NUMBER_FIELDS describing how/whether QUERY is checked?

Comment thread docs/releases/6.2.txt Resolved
@mbeijen

mbeijen commented Sep 2, 2026

Copy link
Copy Markdown
Contributor Author

Hey, thanks for working on this. Not a full review yet, but an early query inquiry for you:

* doc updates for `DATA_UPLOAD_MAX_NUMBER_FIELDS` describing how/whether QUERY is checked?

Well, we do not parse the QUERY body (just as we do not parse PUT body, see https://code.djangoproject.com/ticket/12635) so I'm unsure what we should add here?

@jacobtylerwalls

Copy link
Copy Markdown
Member

Well, we do not parse the QUERY body (just as we do not parse PUT body

Thanks, that answers my question!

@jonbiemond jonbiemond left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks awesome! And is spec compliant (minus caching) as far as I can tell. Just want to note, as was already mentioned on the ticket, that this ticket is currently owned by 송준호 (Song-Junho97) and it would be inappropriate to merge this PR till they have had enough time to respond. In the meantime I have a couple thoughts.

Without caching support the spec is only partially implemented. Is there precedence for releasing a partial implementation? Can we add caching support to this PR? I get that it's a lot for one PR. It would keep things a bit more manageable if this change was broken up over multiple commits.

Comment thread django/contrib/admin/options.py Resolved
Comment thread django/middleware/csrf.py Resolved
Comment thread tests/decorators/test_http.py Resolved
Comment thread tests/generic_views/test_base.py Resolved
Comment thread tests/middleware/tests.py Resolved
Comment thread tests/test_client/tests.py Resolved
@BenA-SA

BenA-SA commented Sep 3, 2026

Copy link
Copy Markdown

Without caching support the spec is only partially implemented. Is there precedence for releasing a partial implementation? Can we add caching support to this PR?

I looked into the caching part when I was comparing my branch to this one, and I'd be happy to pick it up if Michiel would like me to — either into this PR or as a separate follow-up ticket once this lands.

ebremer added a commit to ebremer/RFC10008support that referenced this pull request Sep 4, 2026
The headline is Starlette. The ASGI layer under FastAPI merged QUERY at
+75 -- the maintainer landed his own branch the day he opened it and
closed both community PRs (+9 test client, +20 routing) in its favour.
FastAPI pins starlette>=0.46.0, so the ASGI half of that story is done,
and the discussion (+78) now puts the remaining gate on ReDoc 2
specifically rather than OpenAPI 3.2 generally: Swagger UI supports 3.2,
ReDoc is gated on 3.1 and would not classify query as an operation.

Django got a real PR. The contributor's fork became django/django#21855
at +74 and is drawing fellow review: QUERY content exposed through
HttpRequest.body and deliberately not parsed, the same treatment as PUT,
with the diff reaching admin, auth admin, the common/CSRF/conditional-GET
middleware, utils.cache, the HTTP view decorators and the test client.
The live question is scope -- whether cache/Vary support must land in the
same PR or a follow-up ticket.

Vapor is unblocked. swift-http-types 1.7.0 shipped at +79 with "Make
QUERY method public" (#130), which was the exact condition the approved
Vapor PR was held for. Nothing blocks it now; it simply has not been
touched since +73. SwiftNIO's own PR is the last unmerged piece of the
Swift stack.

Status changes: fasthttp merged (+73), reitit merged (+76) after 69 days
when a maintainer put it in mergeable shape himself, Hanami merged (+80).
Next.js is out of draft but still unreviewed. Helidon changed shape --
after months where Oracle's contributor agreement was the blocker (the
OCA download 404s outside the US), a maintainer said at +79 he will take
the work over as a PR of his own with the contributor as co-author.
Jakarta REST woke up after 62 days: a new contributor volunteered at +79
for the whole scope, annotation through TCK, though the underlying
committer vote has not changed. nghttp2 closed not planned at +77 -- the
stale bot, not a decision; nobody ever replied. oasdiff released in
v1.30.0 (+75). GoFr took a second full review round at +78 and is the
most actively reviewed open QUERY PR in the tracker.

Non-events worth recording: Envoy's merge still has no release (1.40 not
cut, latest v1.39.1 at +72), Spring has cut no milestone since M1 missed
the merge, Slim 4.15.3 shipped at +77 without the 4.x backport even
though the maintainer pushed the fix himself at +67, django-ninja 1.7.0
shipped at +75 without its PR, and Kong's discussion has now sat 46 days
with zero comments.

New rows (14). Servers: Grape (merged same-day at +78, the maintainer
"excited to beat Rack and Rails on QUERY support", and correct, since
Rails' merge is still unreleased), Wolverine, Tempest, APIFlask,
Comunica (merged +77 -- the SPARQL engine many Solid apps sit on, moving
before the protocol spec did), GenHTTP, Marko Run, Lilya. Clients: Kiota
(the maintainer scoped it to adding QUERY to each language's HttpMethod
enum, since arbitrary verbs would be source-breaking), Gleam http.
Tooling: httpYac, hono-openapi, sphinxcontrib-httpdomain, W3C SPARQL
Protocol (closed completed +77 -- not a first-class 1.2 binding, since
1.2 is scoped to RDF 1.2 and errata, but an informative Note mirroring
the existing HTTP PATCH note).

Also: Apache Traffic Server's row now names the fork carrying the work
(JakeChampion/trafficserver#5, merged +69), since nothing has been
proposed upstream; chi's row records that v5.3.1 was tagged the day after
the merge, the fastest request-to-release turnaround here; HTTP::Message
shipped in v7.04 the same day it merged; and Apache HttpClient's alpha1
date is corrected from +52 (the RC3 tag) to +58 (rel/v5.7-alpha1).

All 120 changed star counts refreshed as of 2026-09-04. Every tracked
issue and PR was re-checked against its live state, and release claims
were checked against tags and changelogs rather than inferred from merge
dates.
@mbeijen

mbeijen commented Sep 8, 2026

Copy link
Copy Markdown
Contributor Author

thanks for your suggestions so far. Also I've added a stab at query caching which seems not too hard here --> mbeijen@28b67f5

it might go in a separate PR or maybe we can even embed it here, what do you think?

The main thing about caching QUERY requests is that the cache key is URL, content-type, and body, as specified in the RFC.

@jacobtylerwalls

Copy link
Copy Markdown
Member

Let's keep caching separate for now. If the cache key includes the body, that takes me back to my earlier question about automatic parsing and DoS.

@mbeijen

mbeijen commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Ok! (Also, we should only parse when responding to the QUERY. For the cache lookup we should NOT parse. Just compare the cache keys.)

@jonbiemond jonbiemond left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the changes! Just a couple clarification comments.

Comment thread tests/generic_views/test_base.py Resolved
Comment thread django/contrib/admin/options.py Resolved
@mbeijen

mbeijen commented Sep 9, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review so far! If there are more comments, please let me know, I'll try to be very responsive!

@sarahboyce sarahboyce left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!
I decided to look at the docs a bit this morning 👍

Comment thread docs/ref/request-response.txt Outdated
Comment on lines +81 to +85
Django treats the :rfc:`QUERY <10008#section-2>` method as safe and
idempotent, like ``GET`` and ``HEAD``: it's exempt from CSRF protection and
takes part in conditional request processing. The query content isn't
parsed into :attr:`HttpRequest.POST`; read it from :attr:`HttpRequest.body`
and parse it according to the request's ``Content-Type``.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this fits well here. Perhaps we could mention in HttpRequest.body like:

--- a/docs/ref/request-response.txt
+++ b/docs/ref/request-response.txt
@@ -41,7 +41,8 @@ All attributes should be considered read-only, unless stated otherwise.
     The raw HTTP request body as a bytestring. This is useful for processing
     data in different ways than conventional HTML forms: binary images,
     XML payload etc. For processing conventional form data, use
-    :attr:`HttpRequest.POST`.
+    :attr:`HttpRequest.POST`. For other request content, such as the content of
+    ``PUT`` or ``QUERY`` requests, use this attribute.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the suggestions! I copied them

Comment thread docs/ref/middleware.txt Resolved
Comment thread docs/releases/6.2.txt Outdated

Django now recognizes the :rfc:`QUERY <10008>` method, which performs a safe,
idempotent query with the query content in the request body rather than in the
URL's query string.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
URL's query string.
URL's query string. Use ``QUERY`` for read-only operations whose input is more
naturally expressed in a request body, such as searches with complex,
structured filters.

Perhaps

Comment thread docs/releases/6.2.txt Outdated
Comment on lines +70 to +75
Caching of ``QUERY`` responses is not supported yet. The cache key for a
``QUERY`` request must incorporate the request content, which
:class:`~django.middleware.cache.UpdateCacheMiddleware`,
:class:`~django.middleware.cache.FetchFromCacheMiddleware`, and
:func:`~django.views.decorators.cache.cache_page` don't do, so they continue to
ignore ``QUERY`` requests.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be tempted to say:

Suggested change
Caching of ``QUERY`` responses is not supported yet. The cache key for a
``QUERY`` request must incorporate the request content, which
:class:`~django.middleware.cache.UpdateCacheMiddleware`,
:class:`~django.middleware.cache.FetchFromCacheMiddleware`, and
:func:`~django.views.decorators.cache.cache_page` don't do, so they continue to
ignore ``QUERY`` requests.
:class:`~django.middleware.cache.UpdateCacheMiddleware`,
:class:`~django.middleware.cache.FetchFromCacheMiddleware` and
:func:`~django.views.decorators.cache.cache_page` do not cache ``QUERY``
responses. Conditional request processing with ``ETag`` or ``Last-Modified``
validators is supported.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@jonbiemond jonbiemond left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mbeijen, the documentation looks great! Just have a couple thoughts on sentence structure which I hope will improve accessibility.

Comment thread docs/releases/6.2.txt Resolved
Comment thread docs/releases/6.2.txt Resolved
Comment thread docs/topics/testing/tools.txt Outdated
Comment on lines +417 to +418
and returns a ``Response`` object. Useful for testing endpoints that
accept a safe, idempotent query in the request body.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need the "Useful for..." part? I know it follows the convention of the other methods here. It feels out of place to me.

Comment thread docs/topics/testing/tools.txt Resolved
@mbeijen

mbeijen commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

I think your suggestions look good! I committed them via the web UI on my phone, I am traveling for about another week and did not bring my laptop. Thanks for your suggestions! I would really like to see QUERY in Django!

@jacobtylerwalls jacobtylerwalls left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work. I left very minor feedback. I'm hoping to play with a test project like djangoproject.com and give this a manual test drive, but I don't anticipate having any other substantive feedback.

I would really like to see QUERY in Django!

Me too! 🙌

I suppose I need to start thinking next about how practically we can support this in the cache middleware.

Comment thread tests/test_client/tests.py Resolved
Comment thread tests/generic_views/test_base.py Outdated
content available as request.body.
"""
request = self.rf.query(
"/", data=b"q=django", content_type="application/x-www-form-urlencoded"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm surprised to see our basic test examples here throughout not using the default content_type. Is there a reason you prefer "application/x-www-form-urlencoded" here?

Comment thread docs/releases/6.2.txt Resolved
Comment thread docs/releases/6.2.txt Resolved
Comment thread docs/topics/http/decorators.txt Resolved
Comment thread tests/admin_views/tests.py Resolved
Comment thread docs/ref/middleware.txt Resolved
Comment thread django/test/client.py
**extra,
):
"""Construct a QUERY request."""
data = self._encode_json(data, content_type)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flagging for other reviewers (e.g. @sarahboyce) that we are consciously not raising a mutually exclusive error like we do for GET and HEAD when passing data and query_params together.

From my read of the discussion on #17447, that seems consistent.

Comment thread docs/topics/testing/tools.txt Resolved
@jacobtylerwalls

Copy link
Copy Markdown
Member

I'm hoping to play with a test project

I test drove this on a project I used to work on, which had a classic use case for sending a GET-like request by POST in order to supply GeoJSON geometries via the request body.

The RequestFactory and TestClient worked as I expected for testing the view after I swapped POST for QUERY, and I verified that CSRF protection was off after manually instantiating a Client(enforce_csrf_checks=True). Now calls to this API won't need CSRF tokens, or an exemption! 👏

@mbeijen

mbeijen commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Me too! 🙌

Great! I think I configured the PR to allow changes by contributors. Feel free to make the changes you proposed, they all make sense!

Otherwise please wait a bit as I will have no access to my laptop for another week

@jonbiemond

Copy link
Copy Markdown
Contributor

@mbeijen Enjoy your laptop-free travels! I figure there's no rush, we still have 3 months left till the 6.2 feature freeze.

@jacobtylerwalls Speaking of the feature freeze, would we need the caching part of the spec implemented before we included this work in a release?

@jacobtylerwalls

Copy link
Copy Markdown
Member

I think we should aim to have it for 6.2, but I don't see it as a blocker -- this unit of work is useful without caching, and caching can be done manually or downstream (or even using interesting strategies like the Location header). Do you think that's fair?

I'll open a ticket to discuss the design issues. I'd be happy to prioritize it for review if you wanted to get started, @jonbiemond?

@jonbiemond

Copy link
Copy Markdown
Contributor

@jacobtylerwalls Sounds good! If you find time to create a ticket that would be awesome.

@BenA-SA Are you still interested in working on the caching part of this? You may have the right of first refusal, seeing as you've already begun on it. :)

@jacobtylerwalls

Copy link
Copy Markdown
Member

caching: ticket-37428

@mbeijen

mbeijen commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks. I also already implemented caching in a commit i linked earlier

@mbeijen

mbeijen commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

@mbeijen Enjoy your laptop-free travels! I figure there's no rush, we still have 3 months left till the 6.2 feature freeze.

@jacobtylerwalls Speaking of the feature freeze, would we need the caching part of the spec implemented before we included this work in a release?

I've addressed the review comments now

@jacobtylerwalls jacobtylerwalls left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, this looks great 🥳

I see a couple content_type="application/x-www-form-urlencoded" still in the ClientTest/AsyncClientTest, but maybe it's okay to have them for variety's sake 🤷 .

Nice attention to detail throughout, very easy to review!

Comment thread docs/ref/csrf.txt
'login CSRF', where an attacking site tricks a user's browser into logging into
a site with someone else's credentials, is also covered.

The first defense against CSRF attacks is to ensure that GET requests (and

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm squashing in a little tweak here, since the linked RFC doesn't mention QUERY.

diff --git a/docs/ref/csrf.txt b/docs/ref/csrf.txt
index a921bdbb41..984b376ab5 100644
--- a/docs/ref/csrf.txt
+++ b/docs/ref/csrf.txt
@@ -86,12 +86,12 @@ The CSRF protection is based on the following things:
 This ensures that only forms that have originated from trusted domains can be
 used to POST data back.
 
-It deliberately ignores GET requests (and other requests that are defined as
-'safe' by :rfc:`9110#section-9.2.1`). These requests ought never to have any
-potentially dangerous side effects, and so a CSRF attack with a GET request
-ought to be harmless. :rfc:`9110#section-9.2.1` defines POST, PUT, and DELETE
-as 'unsafe', and all other methods are also assumed to be unsafe, for maximum
-protection.
+It deliberately ignores GET and QUERY requests (and other requests that are
+defined as 'safe' by :rfc:`9110#section-9.2.1`). These requests ought never to
+have any potentially dangerous side effects, and so a CSRF attack with a GET
+request ought to be harmless. :rfc:`9110#section-9.2.1` defines POST, PUT, and
+DELETE as 'unsafe', and all other methods are also assumed to be unsafe, for
+maximum protection.
 
 The CSRF protection cannot protect against man-in-the-middle attacks, so use
 :ref:`HTTPS <security-recommendation-ssl>` with
@@ -101,6 +101,10 @@ the HOST header <host-headers-virtual-hosting>` and that there aren't any
 (because XSS vulnerabilities already let an attacker do anything a CSRF
 vulnerability allows and much worse).
 
+.. versionchanged:: 6.2
+
+    ``QUERY`` was added to the exempt methods.
+
 .. admonition:: Removing the ``Referer`` header
 
     To avoid disclosing the referrer URL to third-party sites, you might want

QUERY (RFC 10008) is a safe, idempotent method that carries the query in
the request content rather than in the URI's query component. Treat it as
safe throughout: exempt it from CSRF protection, evaluate request
preconditions as for GET, accept it in require_safe(), and dispatch it
from View. Query content is available as HttpRequest.body and is not
parsed into HttpRequest.POST.

Caching is left for a follow-up. Per RFC 10008 Section 2.7 the cache key
for a QUERY request must incorporate the request content, which the cache
middleware and cache_page() don't do, so they continue to ignore QUERY.

Thanks Ben Atkinson, Jonathan Biemond, and Jacob Walls for reviews.
@github-actions

Copy link
Copy Markdown

Uncovered lines in changed files

-------------
django/contrib/admin/options.py (100%)
django/contrib/auth/admin.py (100%)
django/middleware/common.py (100%)
django/middleware/csrf.py (100%)
django/middleware/http.py (100%)
django/test/client.py (100%)
django/utils/cache.py (100%)
django/views/decorators/http.py (100%)
django/views/generic/base.py (100%)
-------------
Total:   28 lines
Missing: 0 lines
Coverage: 100%
-------------

Note: Missing lines are warnings only. Some database-specific lines may not be measured. More information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

coverage Generate a comment with a coverage diff

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants