# Suggestion: Adding \`params\` to TestClient HTTP methods

**URL:** <https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246>\
**Category:** Django Internals\
**Created:** [May 30, 2023, 12:32pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246 "2023-05-30T12:32:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [May 30, 2023, 12:32pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/1 "2023-05-30T12:32:12Z")

</div>

Using [Client.get](https://docs.djangoproject.com/en/4.2/topics/testing/tools/#django.test.Client.get) it’s very easy to add new query parameters to the fake request, you can simply provide them to `data` argument. Very nice and ergonomic.

However, for the other HTTP methods, like `post`, this doesn’t work, since of course they use the same parameter for their form data.

Would it make sense to provide a new `params` argument to all Client HTTP methods, that would work similar to the `requests` / `httpx` libraries?

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [May 31, 2023, 7:08am UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/2 "2023-05-31T07:08:28Z")

</div>

It could! I usually use `urlencode` to put dynamic parameters in the URL:

```auto
resp = self.client.post("/widget/" + urlencode({"user": user.id}))

```

But this is hard to remember, and can lead to random failures if testing with random data that sometimes needs escaping.

Having a parameter for URL parameters would be neat. I’d suggest we use the name `query_params`, rather than just `params`, as per the ongoing Google Summer of Code project that will rename `request.GET` to `request.query_params` ( [[GSOC 2023] Add Configurable Content Type Parsing and Modernise Request Object](https://forum.djangoproject.com/t/gsoc-2023-add-configurable-content-type-parsing-and-modernise-request-object/20933) ).

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [June 2, 2023, 4:42pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/3 "2023-06-02T16:42:47Z")

</div>

It think that makes a lot of sense.

`params` will probably be most familiar to developers used to `requests` and similar, and is a little shorter, but internal consistency with Django itself is even better. Makes for good consistency with `request.user`, which can also be set via Client.

---

<div class="post-metadata">

**Author:** ![adamchainz](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/adamchainz/32/26_2.png) [@adamchainz](https://forum.djangoproject.com/u/adamchainz)\
**Post date:** [June 4, 2023, 9:41pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/4 "2023-06-04T21:41:27Z")

</div>

Great. Would you like to make a ticket?

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [June 7, 2023, 7:55pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/5 "2023-06-07T19:55:12Z")

</div>

Ticket created! [#34640 (Add query\_params argument to test Client methods) – Django](https://code.djangoproject.com/ticket/34640)

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [June 8, 2023, 11:57am UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/6 "2023-06-08T11:57:30Z")

</div>

Seems like this was already requested 11 years ago and closed as wontfix… so technically a duplicate.

As an aside, what’s Django’s policy when it comes to re-assessing old ideas? Do we ever reconsider, or is a wontfix a wontfix?

---

<div class="post-metadata">

**Author:** ![r3dm1ke](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/r3dm1ke/32/14113_2.png) [@r3dm1ke](https://forum.djangoproject.com/u/r3dm1ke)\
**Post date:** [June 8, 2023, 7:43pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/7 "2023-06-08T19:43:35Z")

</div>

I think that query parameters should (per REST philosophy) be used only for get requests and it would make little sense to use them in POST/PUT requests, as the data is sent using other means in that case, and adding an extra argument to the test client would unnecessary (imo) complicate it.

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [June 8, 2023, 10:42pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/8 "2023-06-08T22:42:36Z")

</div>

Note that Django views can be used for much more than only REST APIs, and in the wild plenty of views take both query parameters and POST data.

A few quick examples:

- [django-cms/views.py at develop · django-cms/django-cms · GitHub](https://github.com/django-cms/django-cms/blob/develop/cms/views.py#LL205C27-L205C30) (various types of `?next` is very useful to put in query params)
- [sentry/locale.py at master · getsentry/sentry · GitHub](https://github.com/getsentry/sentry/blob/master/src/sentry/middleware/locale.py#L26) (overriding language code also very useful)
- [wagtail/views.py at main · wagtail/wagtail · GitHub](https://github.com/wagtail/wagtail/blob/main/wagtail/contrib/redirects/views.py#L213)

Although, talking about “REST philosophy”… 🤔 what about the REST pattern says that only GET / DELETE requests should have query parameters? It’s the first time I’ve heard that suggested.

---

<div class="post-metadata">

**Author:** ![r3dm1ke](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/r3dm1ke/32/14113_2.png) [@r3dm1ke](https://forum.djangoproject.com/u/r3dm1ke)\
**Post date:** [June 9, 2023, 7:04pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/9 "2023-06-09T19:04:54Z")

</div>

When talking about REST philosophy, I meant this: GET requests are queries, i.e. they query data. The rest of them, like POST, PUT, PATCH, DELETE are mutations, they do not get the data, they modify it on the server. Therefore, query parameters are only meant for GET requests, as they are _query_ parameters

---

<div class="post-metadata">

**Author:** ![apollo13](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/apollo13/32/30_2.png) [@apollo13](https://forum.djangoproject.com/u/apollo13)\
**Post date:** [June 9, 2023, 7:32pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/10 "2023-06-09T19:32:27Z")

</div>

Philosophy or not, there is nothing in the specs forbidding a POST request with body data and query params. I doubt it is often used, but that doesn’t necessarily mean that we should limit our code.

---

<div class="post-metadata">

**Author:** ![g4borg](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/g4borg/32/16243_2.png) [@g4borg](https://forum.djangoproject.com/u/g4borg)\
**Post date:** [October 3, 2023, 1:09pm UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/11 "2023-10-03T13:09:36Z")

</div>

The Django Test Client is not just used for REST, and yes, there are usecases, where GET params get used in APIs even on POST. It is after all allowed, and is e.g. a very nice way for proxies to add s2s data on top, or to make the calls themselves leave obvious traces in the logs, that do not interfere with the underlying POST/PUT functionality.

The main reason however is, that re-using data in .get already seems inconsistent with reality and other frameworks, and it would be much more explicit, to use GET params with another key-word, because it is actually a different type of parameter than the body encoded data.

In my opinion, I would rather use a params keyword of any sort even on client.get() right now, as it is less confusing, and simply more correct.

---

<div class="post-metadata">

**Author:** ![Zerotask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/zerotask/32/10701_2.png) [@Zerotask](https://forum.djangoproject.com/u/Zerotask)\
**Post date:** [January 8, 2025, 12:16am UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/12 "2025-01-08T00:16:02Z")

</div>

Django 5.1 now offers a _query\_params_ parameter:

> The [`RequestFactory`](https://docs.djangoproject.com/en/5.1/topics/testing/advanced/#django.test.RequestFactory), [`AsyncRequestFactory`](https://docs.djangoproject.com/en/5.1/topics/testing/advanced/#django.test.AsyncRequestFactory), [`Client`](https://docs.djangoproject.com/en/5.1/topics/testing/tools/#django.test.Client), and [`AsyncClient`](https://docs.djangoproject.com/en/5.1/topics/testing/tools/#django.test.AsyncClient) classes now support the `query_params` parameter, which accepts a dictionary of query string keys and values. This allows setting query strings on any HTTP methods more easily.

> **[Django 5.1 release notes | Django documentation](https://docs.djangoproject.com/en/5.1/releases/5.1/#tests)**
>
> The web framework for perfectionists with deadlines.

---

<div class="post-metadata">

**Author:** ![strokirk](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strokirk/32/3414_2.png) [@strokirk](https://forum.djangoproject.com/u/strokirk)\
**Post date:** [January 29, 2025, 9:24am UTC](https://forum.djangoproject.com/t/suggestion-adding-params-to-testclient-http-methods/21246/13 "2025-01-29T09:24:42Z")

</div>

Wow, super! Makes me very happy! 😊
