python으로 request.post에 data에 JSON 데이터를 넘겼을 때, 아래와 같은 에러가 발생함.
ValueError: too many values to unpack (expected 2)
JSON List Array인데, 내부에 key-value pair가 여러개 있으면 안된다.
즉, [{"K1":"V", "K2":"V2","K3":"V3"}, {"K1":"V", "K2":"V2","K3":"V3"}] 이 형태는 안됨.
https://requests.readthedocs.io/en/latest/api/
- data – (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the Request.
이런 경우 그냥 data로 주지 말고, json으로 보내면 해결 됨.
물론 받는 곳에서도 get_json()으로 하면됨.
# request.post(data=params) <-- 이렇게 하지 말고
request.post(json=params)
# 받는 곳에서...
jsonData = request.get_json()
Traceback (most recent call last):
File "/home/my_user/workspace/my_test_deal/my_test_crontab.py", line 3, in <module>
c.run()
File "/home/my_user/workspace/my_test_deal/my_test.py", line 203, in run
response = requests.post("http://127.0.0.1:58889/sendtomejson", params=params, data=datas)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/api.py", line 115, in post
return request("post", url, data=data, json=json, **kwargs)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/sessions.py", line 573, in request
prep = self.prepare_request(req)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/sessions.py", line 484, in prepare_request
p.prepare(
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/models.py", line 371, in prepare
self.prepare_body(data, files, json)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/models.py", line 559, in prepare_body
body = self._encode_params(data)
File "/home/my_user/anaconda3/envs/my_proj/lib/python3.8/site-packages/requests/models.py", line 121, in _encode_params
for k, vs in to_key_val_list(data):
ValueError: too many values to unpack (expected 2)
참고
https://requests.readthedocs.io/en/latest/api/
Developer Interface — Requests 2.28.2 documentation
Developer Interface This part of the documentation covers all the interfaces of Requests. For parts where Requests depends on external libraries, we document the most important right here and provide links to the canonical documentation. Main Interface All
requests.readthedocs.io
'What I Do > 프로그래밍' 카테고리의 다른 글
keras에서 layer의 weight를 업데이트하지 않고 고정하기 (0) | 2023.01.26 |
---|---|
grep을 쓰다 보면 매칭된 부분의 위, 아래 몇 줄을 더 보고 싶을 때가 있다. (0) | 2023.01.17 |
pandas] DataFrame vs. Series (0) | 2023.01.17 |
Java script 아마추어 (0) | 2022.11.14 |