2017-09-12 20 views
0

私はaiohttpを使用するいくつかのアプリケーションを持っています。Python 3.6:aiohttpリクエストからJSONを取得

私は例えば、approptiateエンドポイントにPOSTリクエストを送っ:と同様のデータを持つ

POST mysite.com/someendpoind/ 

{"param1": "value1", "param2": "value2", ..., "paramn": None} 

を次にバックエンド側では、私はこの要求にいくつかの追加条件を追加したい:

data = await request.json() 
data["additional_conditional"] = True 

ただし、request.json()はエラーで失敗します。

[ERROR] Error handling request 
Traceback (most recent call last): 
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web_protocol.py", line 422, in start 
resp = yield from self._request_handler(request) 
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web.py", line 306, in _handle 
resp = yield from handler(request) 
File "/usr/local/lib/python3.5/dist-packages/aiohttp_session/__init__.py", line 129, in middleware 
response = yield from handler(request) 
File "/opt/bikeamp/auth/__init__.py", line 57, in wrapped 
return (yield from f(request, user)) 
File "<my_module>.py", line 185, in <my_func> 
data_json = await request.json() 
File "/usr/local/lib/python3.5/dist-packages/aiohttp/web_request.py", line 469, in json 
return loads(body) 
File "/usr/lib/python3.5/json/__init__.py", line 319, in loads 
return _default_decoder.decode(s) 
File "/usr/lib/python3.5/json/decoder.py", line 339, in decode 
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 
File "/usr/lib/python3.5/json/decoder.py", line 357, in raw_decode 
raise JSONDecodeError("Expecting value", s, err.value) from None 
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) 

私は私の要求の内容で何とかかどうか確認することを決定した後:

await request.read() 

b'field1=value1&field2=value2&field3=value3&field4=&field5=&field6=' 

だから、私はわからないんだけど、問題は、空のパラメータを持つかもしれません。

また、私は経由して、このデータを取得しようとしていた。

data = await request.post() 
data["additional_condition"] = True 

しかし、これはMultiDictProxyを返します。 Pythonはこれらのオブジェクトをpickleできません。

既知の解決策はありますか?

答えて

0

ポストは{"email": "[email protected]"}のようなものがでそれを確認した場合、私は、同じ問題を持っていた:

@router('/', methods=['POST', ]) 
    async def post_request(request):  

     post = await request.post() 
     email = post.get('email') # because it's MultiDict 
     logging.warning(post)  # see post details 
     logging.warning(email)  # shows value "[email protected]" 

     json = await request.text() # 
     logging.warning(json)  # shows json if it was ajax post request 
関連する問題