【问题标题】:Nested Dictionaries to JSON for the POST request Python将字典嵌套到 JSON 以用于 POST 请求 Python
【发布时间】:2017-09-21 20:17:25
【问题描述】:

我在转换嵌套字典形式的有效负载数据以使用 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'}} 

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

【问题讨论】:

  • 这是什么 API?有文档吗?如果是公共 API,您点击的 url 会有所帮助
  • 您在消息中收到了“提交的 XML”,所以 api 请求可能是 XML 数据?或者你应该在你的标题中设置Content-Typeapplication/json。如heinst所说,如果可以的话,请给我们文档或API的名称。
  • @rsz: 我已经在标题中将 'content-type' 设置为 'application/json'

标签: python json dictionary post python-requests


【解决方案1】:

我有类似的问题,令人沮丧,但我解决了。 Python 请求不适用于嵌套的 json,它们需要一层 json。 它像 form(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..

【讨论】:

  • 感谢!应该被选为接受的答案。
  • 谢谢你,让我从一个多星期的挫折中解脱出来。我不知道请求中的嵌套 json 限制。
【解决方案2】:

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

Web 服务很有可能假定您正在向其传递 XML,除非您通过将 Content-Type 设置为 application/json 来指定您正在传递 JSON。 (这个 Web API 也可能真的需要 XML,服务的文档会告诉你)

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

【讨论】:

  • @RyanWicox:谢谢你的建议。那么我应该以我在这里提到的相同形式使用有效载荷还是需要更改结构?
  • 我收到以下错误:TypeError: request() got an unexpected keyword argument 'json'。
  • 在requests 2.4.2中添加了json参数,你用的可能是之前的版本吗???/
  • 同样的错误。我已将软件包更新到版本 2.13.0
猜你喜欢
  • 2021-03-12
  • 2021-02-07
  • 2015-01-23
  • 2021-09-18
  • 2018-05-31
  • 1970-01-01
  • 1970-01-01
  • 2021-02-02
  • 1970-01-01
相关资源
最近更新 更多