为POST请求将嵌套字典转换为JSON Python

3mpgtkmj  于 2023-05-02  发布在  Python
关注(0)|答案(3)|浏览(120)

我在转换嵌套字典形式的有效负载数据时遇到了麻烦,无法使用Python requests模块将其作为POST请求的数据传递。表单数据如下:

payload = {'request':  {
                'appkey': "936725A4-7D9A-11E5-81AC-86EC8D89CD5A"},
            'formdata':{
                    'currency':'US',
                    'dataview':'store_default',
                    'distinct':'_distance, clientkey',
                    'geolocs':{
                            'geoloc':[{
                                    '0':{
                                            'address1':'',
                                            'addressline':'19128, PA',
                                            'city':'Philadelphia',
                                            'country':'US',
                                            'latitude':'40.0532987',
                                            'longitude':'-75.23040379999998',
                                            'postalcode':'19128',
                                            'province':'',
                                            'state':'PA'}}]
                            },
                    'google_autocomplete':'true',
                    'limit':'250',
                    'nobf':'1',
                    'searchradius':'15|25|50|100|250|350|450|550|650|750|850|950',
                    'true':'1',
                    'where':{'partner_reseller': {'eq':'1'}}}                    
          }

r = requests.post(url,data=simplejson.dumps(payload),headers=header)
result = simplejson.loads(str(r.content))

有人可以帮助我的结构,并能指出我所写的错误。我一直得到以下错误:

{'code': 1008,
 'response': {'message': 'The submitted XML is not properly formed'}}

我将非常感谢你的帮助。谢谢大家。

ffvjumwh

ffvjumwh1#

我也遇到过类似的问题,很沮丧,但我解决了。Python请求不适用于嵌套的json,它们期望一层json。它像表单(application/x-www-form-urlencoded)一样处理

# Wrong
data = {'param1': {'a':[100, 200]},
        'param2': 'value2',
        'param3': False}

# You have to convert values into string:
data = {'param1': json.dumps({'a':[100, 200]}),
        'param2': 'value2',
        'param3': json.dumps(False)}

在您的案例中:

import json
params = {
        'appkey': "936725A4-7D9A-11E5-81AC-86EC8D89CD5A"},
        'formdata':{
            'currency':'US',
            'dataview':'store_default',
            'distinct':'_distance, clientkey',
            'geolocs':{
                    'geoloc':[{
                            '0':{
                                    'address1':'',
                                    'addressline':'19128, PA',
                                    'city':'Philadelphia',
                                    'country':'US',
                                    'latitude':'40.0532987',
                                    'longitude':'-75.23040379999998',
                                    'postalcode':'19128',
                                    'province':'',
                                    'state':'PA'}}]
                    },
            'google_autocomplete':'true',
            'limit':'250',
            'nobf':'1',
            'searchradius':'15|25|50|100|250|350|450|550|650|750|850|950',
            'true':'1',
            'where':{'partner_reseller': {'eq':'1'}}}                    
          }
payload = {'request':  json.dumps(params) }

r = requests.post(url,data=payload) # important to keep payload as json!!!
result = r.text # or depends what the return is..
nhjlsmyf

nhjlsmyf2#

我的建议是使用JSON参数,让请求将对象编码为JSON,并让请求将Content-Type头设置为application/json
Web服务很可能假定您传递的是XML,除非您通过将Content-Type设置为application/json指定传递JSON。(这个web API也可能真的需要XML,服务的文档会告诉你)
requests.post(url,json=payload,headers=header)

sc4hvdpw

sc4hvdpw3#

从前面的两个答案中获得灵感,我个人认为更简洁的另一种方法是将主体序列化为字符串,并将Content-Type头设置为application/json

import json, requests

#Other stuff

headers = { "Content-Type": "application/json" }
data = json.dumps(payload)

r = requests.post(url,data=data,headers=headers)

相关问题