Skip to content

Conversation

@MDziwny
Copy link
Contributor

@MDziwny MDziwny commented Sep 21, 2020

fix #1330

"/",
data=data,
content_type="application/json",
CONTENT_TYPE="application/json", # Force django test client to add the content-type even if no data
Copy link
Member

Choose a reason for hiding this comment

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

Are both lines necessary? Or is it possible to remove one of the content_type parameters? (I didn't check what they do, do maybe it isn't a very smart question.)

Copy link
Member

Choose a reason for hiding this comment

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

I think the second is to force the header option.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes I see. The first makes Django automatically encode the data as JSON, the second adds the header even if data is falsy.

It's certainly convenient that Django automatically encodes the data if the content_type has some particular value. Sometimes the interdependence of different arguments just isn't obvious (enough) to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, unfortunately, this looks more like a trick, the django client don't add the content_type if there is no data, so I add manually (extra kwargs which are not known by the test client are considered as extra headers).

@matthiask
Copy link
Member

Thanks, this looks very good!

@matthiask matthiask merged commit ba2e6ba into django-commons:master Sep 21, 2020
@israel-tk
Copy link

If you send content type = json but don't send JSON the system fails as well. Pseudo-code in django-debug-toolbar 3.1.1:

>>> Client(enforce_csrf_checks=True).post(
            "some-valid-url", data="invalid-json", content_type="application/json",
        )
/usr/local/lib/python3.8/site-packages/django/test/client.py:742: in post
    response = super().post(path, data=data, content_type=content_type, secure=secure, **extra)
/usr/local/lib/python3.8/site-packages/django/test/client.py:404: in post
    return self.generic('POST', path, post_data, content_type,
/usr/local/lib/python3.8/site-packages/django/test/client.py:470: in generic
    return self.request(**r)
/usr/local/lib/python3.8/site-packages/django/test/client.py:710: in request
    self.check_exception(response)
/usr/local/lib/python3.8/site-packages/django/test/client.py:571: in check_exception
    raise exc_value
/usr/local/lib/python3.8/site-packages/django/core/handlers/exception.py:47: in inner
    response = get_response(request)
/usr/local/lib/python3.8/site-packages/debug_toolbar/middleware.py:67: in __call__
    panel.generate_stats(request, response)
/usr/local/lib/python3.8/site-packages/debug_toolbar/panels/history/panel.py:57: in generate_stats
    data = json.loads(
/usr/local/lib/python3.8/json/__init__.py:357: in loads
    return _default_decoder.decode(s)
/usr/local/lib/python3.8/json/decoder.py:337: in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

@matthiask
Copy link
Member

Oh right, that's a new case. Do you want submit a pull request @israel-tk ? Or maybe at least open an issue so that this problem isn't forgotten.

@israel-tk
Copy link

Oh right, that's a new case. Do you want submit a pull request @israel-tk ? Or maybe at least open an issue so that this problem isn't forgotten.

Done creating a new issue: #1403

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Empty request with json content type fails with a JSONDecodeError when using the history panel

4 participants