APIClient format=json encodes bool values to strings #8038
Unanswered
mastacheata
asked this question in
Potential Issue
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey there,
I'm trying to send a boolean value from a test using the APIClient from DRF and using the format=json parameter to auto-encode the payload to JSON.
When it arrives in the serializer's initial_data it's then automatically turned into a string. This does not happen when I send the request using JS or curl and manually encode the JSON to have a boolean value inside of a JSON object and it also doesn't occur when I set the content_type on the APIClient and encode the payload by hand using json.dumps instead.
Here's some simplified/reduced example code:
In the test I have this snippet:
In the serializer, the skip_check field is not defined, but we access it from a field-level validation method (i.e. validate_field):
If instead of using format=json and passing the payload dictionary directly to the client.post method, I pass content_type='application/json' and encode the payload with json,dumps, everything behaves as expected (True is recognized as a boolean by the Serializer):
I can't seem to find any reason why the built-in JSON Formatter might behave differently in this instance than the primitive json,dumps approach....
Beta Was this translation helpful? Give feedback.
All reactions