connexion
connexion copied to clipboard
Default Content-Type with curl and aiohttp returns a wrong error message
Description
Everything works if you send an API request as
curl --data '{...}' -H "Content-Type: application/json" http://...
but everything doesn't work if you don't specify the Content-Type header:
curl --data '{...}' http://...
The returned error is 400 - '' is not of type 'object'.
This happens because the recent PR has the following code: https://github.com/spec-first/connexion/pull/1222/files#diff-125f60cd18eebd613caac8f881d4dd9b5002244e19bd22ada80425495a1c976fR328
# Note: if request is not 'application/x-www-form-urlencoded' nor 'multipart/form-data',
# then `post_data` will be left an empty dict and the stream will not be consumed.
post_data = await req.post()
It turns out that if there is no Content-Type header, curl sets it to application/x-www-form-urlencoded. The body becomes empty, and subsequent json deserialization fails.
Expected behaviour
Better error message, e.g. "Content-Type header is required or is wrong".
Actual behaviour
The returned error is 400 - '' is not of type 'object'.
Hi @vmarkovtsev, thanks for the report.
So if I understand correctly:
- The spec defines content-type
application/json - Curl sends content-type
application/x-www-form-urlencoded - The error is
400 - '' is not of type 'object'instead of 415 error?
Would be great if you could post a minimal working example.
Yes, all is correct! I have something like this in the spec:
paths:
/foo_bar:
put:
operationId: foo_bar
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/FooBar'
x-body-name: body
responses:
200:
content:
application/json:
schema:
type: object
description: Test API.
and
async def foo_bar(request, body):
# doesn't matter
return None
Fixed since https://github.com/spec-first/connexion/pull/1588