发送字典列表作为字典的值 requests.post 出错
Sending list of dicts as value of dict with requests.post going wrong
我有客户端服务器应用程序。
我定位了问题并有这样的逻辑:
客户:
# -*- coding: utf-8 -*-
import requests
def fixing:
response = requests.post('http://url_for_auth/', data={'client_id': 'client_id',
'client_secret':'its_secret', 'grant_type': 'password',
'username': 'user', 'password': 'password'})
f = response.json()
data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12',
'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]}
data.update(f)
response = requests.post('http://url_for_working/, data=data)
response.text #There I have an Error about which I will say later
oAuth2 运行良好。但是在服务器端,我在 request.data
中没有产品
<QueryDict: {u'token_type': [u'type_is_ok'], u'access_token': [u'token_is_ok'],
u'expires_in': [u'36000'], u'coordinate_y': [u'8.4'],
u'coordinate_x': [u'12.3'], u'products': [u'count', u'id', u'count',
u'id'], u'address': [u'\u041c, 12'], u'scope': [u'read write'],
u'refresh_token': [u'token_is_ok']}>
QueryDict的这部分让我很难过...
'products': [u'count', u'id', u'count', u'id']
然后当我尝试制作 python 字典时:
request.data.dict()
... u'products': u'id', ...
并且可以肯定的是,其他字段与 Django 序列化程序的验证配合良好。但不是那样,因为我有错误的价值观。
看起来像请求(因为它有 x-www-encoded-form 默认值)不能包含字典列表作为字典中键的值所以...我应该在这种情况下使用 json。
最后我做了这个函数:
import requests
import json
def fixing:
response = requests.post('http://url_for_auth/', data={'client_id': 'client_id',
'client_secret':'its_secret', 'grant_type': 'password',
'username': 'user', 'password': 'password'})
f = response.json()
headers = {'authorization': f['token_type'].encode('utf-8')+' '+f['access_token'].encode('utf-8'),
'Content-Type': 'application/json'}
data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12',
'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]}
response = requests.post('http://url_for_working/', data=json.dumps(data),
headers=headers)
response.text
我得到了正确的回应。
已解决!
你好我想刷新这个话题,因为我有类似的问题,上面的解决方案对我不起作用。
import requests
import urllib.request
import pprint
import json
from requests import auth
from requests.models import HTTPBasicAuth
payload = {
'description': 'zxcy',
'tags':[{
'id': 22,
'label': 'Card'}]
}
files = {'file': open('JAM5.pdf','rb')}
client_id = 32590
response = requests.post('https://system...+str(client_id)' , files=files ,data=payload, auth=HTTPBasicAuth(...)
以上代码成功将文件添加到 CRM 系统并将描述添加到添加的文件,但我也必须为此添加标签,而且它s seems doesn
根本不起作用
当我尝试使用 data=json.dumps(payload) 时,我得到了这个:
raise ValueError("Data must not be a string.")
ValueError: Data must not be a string.
我有客户端服务器应用程序。 我定位了问题并有这样的逻辑:
客户:
# -*- coding: utf-8 -*-
import requests
def fixing:
response = requests.post('http://url_for_auth/', data={'client_id': 'client_id',
'client_secret':'its_secret', 'grant_type': 'password',
'username': 'user', 'password': 'password'})
f = response.json()
data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12',
'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]}
data.update(f)
response = requests.post('http://url_for_working/, data=data)
response.text #There I have an Error about which I will say later
oAuth2 运行良好。但是在服务器端,我在 request.data
中没有产品<QueryDict: {u'token_type': [u'type_is_ok'], u'access_token': [u'token_is_ok'],
u'expires_in': [u'36000'], u'coordinate_y': [u'8.4'],
u'coordinate_x': [u'12.3'], u'products': [u'count', u'id', u'count',
u'id'], u'address': [u'\u041c, 12'], u'scope': [u'read write'],
u'refresh_token': [u'token_is_ok']}>
QueryDict的这部分让我很难过...
'products': [u'count', u'id', u'count', u'id']
然后当我尝试制作 python 字典时:
request.data.dict()
... u'products': u'id', ...
并且可以肯定的是,其他字段与 Django 序列化程序的验证配合良好。但不是那样,因为我有错误的价值观。
看起来像请求(因为它有 x-www-encoded-form 默认值)不能包含字典列表作为字典中键的值所以...我应该在这种情况下使用 json。 最后我做了这个函数:
import requests
import json
def fixing:
response = requests.post('http://url_for_auth/', data={'client_id': 'client_id',
'client_secret':'its_secret', 'grant_type': 'password',
'username': 'user', 'password': 'password'})
f = response.json()
headers = {'authorization': f['token_type'].encode('utf-8')+' '+f['access_token'].encode('utf-8'),
'Content-Type': 'application/json'}
data = {'coordinate_x': 12.3, 'coordinate_y': 8.4, 'address': u'\u041c, 12',
'products': [{'count': 1, 'id': 's123'},{'count': 2, 'id': 's124'}]}
response = requests.post('http://url_for_working/', data=json.dumps(data),
headers=headers)
response.text
我得到了正确的回应。 已解决!
你好我想刷新这个话题,因为我有类似的问题,上面的解决方案对我不起作用。
import requests
import urllib.request
import pprint
import json
from requests import auth
from requests.models import HTTPBasicAuth
payload = {
'description': 'zxcy',
'tags':[{
'id': 22,
'label': 'Card'}]
}
files = {'file': open('JAM5.pdf','rb')}
client_id = 32590
response = requests.post('https://system...+str(client_id)' , files=files ,data=payload, auth=HTTPBasicAuth(...)
以上代码成功将文件添加到 CRM 系统并将描述添加到添加的文件,但我也必须为此添加标签,而且它s seems doesn
根本不起作用
当我尝试使用 data=json.dumps(payload) 时,我得到了这个:
raise ValueError("Data must not be a string.")
ValueError: Data must not be a string.