Talking only from my experience here, but please note that it should be preferred to use the json
field with the dict, rather than dumping the dict in the data
field.
Again, talking only from experience, I did not study the code itself, but it seems that the requests library does some more clever json serialization than just json.dumps
. When using json.dumps
in data
field, I have encountered several instances where this resulted in an "value is not a valid dict" error response from the (FastAPI) server. Using the json
field instead fixed these issues.
EDIT: I went through the code today. If you use the json
parameter, it seems the requests library really only sets Content-Type
and dumps it:
from .compat import json as complexjson
content_type = 'application/json'
body = complexjson.dumps(json)
if not isinstance(body, bytes):
body = body.encode('utf-8')
where in the requests.compat
, the json
is just:
try:
import simplejson as json
except ImportError:
import json
... therefore I really cannot figure out why doing this manually using the data
parameter just sometimes fails to work. ¯\_(ツ)_/¯