bauerji / flask-pydantic

flask extension for integration with the awesome pydantic package
MIT License
368 stars 58 forks source link

When I use GET and the Content-Type in the header is application / json, body_params = request.get_json () in the core file will report werkzeug.exceptions.BadRequest #2

Closed nathan77886 closed 4 years ago

nathan77886 commented 4 years ago

When I use GET and the Content-Type in the header is application / json, body_params = request.get_json () in the core file will report werkzeug.exceptions.BadRequest

[2020-01-15 14:00:21] [ERROR] [logs] [__call__] [52] Traceback (most recent call last):
  File "D:\Development_language\pyenv\api\lib\site-packages\werkzeug\wrappers\json.py", line 119, in get_json
    rv = self.json_module.loads(data)
  File "D:\Development_language\pyenv\api\lib\site-packages\flask\json\__init__.py", line 253, in loads
    return _json.loads(s, **kwargs)
  File "d:\development_language\pyhton37\Lib\json\__init__.py", line 361, in loads
    return cls(**kw).decode(s)
  File "d:\development_language\pyhton37\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "d:\development_language\pyhton37\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:\isizy\services\api\app\models\logs.py", line 49, in __call__
    response = self._func(*args, **kwargs)  # 执行api
  File "D:\isizy\services\api\api_lib\flask_pydantic\core.py", line 116, in wrapper
    body_params = request.get_json()
  File "D:\Development_language\pyenv\api\lib\site-packages\werkzeug\wrappers\json.py", line 128, in get_json
    rv = self.on_json_loading_failed(e)
  File "D:\Development_language\pyenv\api\lib\site-packages\flask\wrappers.py", line 27, in on_json_loading_failed
    raise BadRequest()
werkzeug.exceptions.BadRequest: 400 Bad Request: The browser (or proxy) sent a request that this server could not understand.

[2020-01-15 14:00:21] [ERROR] [wsgi] [_log] [196] 500 GET /api/scores/da1c17ae-1d4e-11ea-be9b-0242ac130005/trend/data (192.168.1.45) 381.94ms

This is my request: image

bauerji commented 4 years ago

Hi, I am sorry I probably don't quite understand the situation here. Could you please check that the request body you are sending is in valid JSON format? And could you provide me the JSON body that's causing this issue? Thanks Jirka

nathan77886 commented 4 years ago

like this: flask:

class Other(BaseModel):
    something:str

@api_old.route('/api/other',methods=['GET'])
@ApiCallReport
@validate(query=Other)
def something():
    something = request.query_params.something
    return something

requests:

import requests

header = {
    'Content-Type':'application/json'
}

url = '''http://192.168.1.45/v2/api/other?something=hello'''

response = requests.get(url,headers=header)

print(response.text)

in my response:

{
"info":"400 Bad Request: The browser (or proxy) sent a request that this server could not understand.",
"message":"error"
}
bauerji commented 4 years ago

There was a problem caused by accessing body parameters even if there was no model defined. It is fixed in this commit.

The new version (0.0.5) should work fine.

Thank you for your feedback.

Cheers Jirka