嵌套字典到JSON以获取POST请求Python [英] Nested Dictionaries to JSON for the POST request Python

查看:115
本文介绍了嵌套字典到JSON以获取POST请求Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法转换嵌套字典形式的有效载荷数据,以使用Python请求模块将其作为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'}} 

非常感谢您的帮助.谢谢.

解决方案

我的建议是使用JSON参数,并让请求均将对象编码为JSON,并让请求将Content-Type标头设置为application/json.

除非您通过将Content-Type设置为application/json来指定要传递JSON,否则该Web服务很有可能假定您正在传递XML. (也有可能该Web API确实也想要XML,该服务的文档会告诉您)

requests.post(url,json=payload,headers=header)

I am having trouble converting the payload data in the form of nested dictionaries to pass it as a data for the POST request using Python requests module. The form data is as below:

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))

Can somebody please assist me with structure and can point out the mistake in what I have written. I keep getting the following error:

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

I'll appreciate your help a lot. Thank you.

解决方案

My suggestion is to use the JSON parameter and let requests both encode the object to JSON and let requests set the Content-Type header to application/json.

It's very possible that the web service assumes you're passing it XML, unless you specify that you're passing JSON, via setting the Content-Type to application/json. (It's also possible this web API really wants XML too, the docs for the service would tell you)

requests.post(url,json=payload,headers=header)

这篇关于嵌套字典到JSON以获取POST请求Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆