【问题标题】:How to POST JSON data with Python Requests?如何使用 Python 请求发布 JSON 数据?
【发布时间】:2022-01-23 02:09:37
【问题描述】:

我需要将一个 JSON 从客户端发送到服务器。我正在使用 Python 2.7.1 和 simplejson。客户端正在使用请求。服务器是 CherryPy。我可以从服务器获取硬编码的 JSON(代码未显示),但是当我尝试将 JSON 发布到服务器时,我得到“400 Bad Request”。

这是我的客户端代码:

data = {'sender':   'Alice',
    'receiver': 'Bob',
    'message':  'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)

这是服务器代码。

class Root(object):

    def __init__(self, content):
        self.content = content
        print self.content  # this works

    exposed = True

    def GET(self):
        cherrypy.response.headers['Content-Type'] = 'application/json'
        return simplejson.dumps(self.content)

    def POST(self):
        self.content = simplejson.loads(cherrypy.request.body.read())

有什么想法吗?

【问题讨论】:

  • 我使用的是直接来自documentation 的示例的精简版本。
  • 我的评论仍然有效 - CherryPy 不会使用 content 参数调用类 __init__ 方法(并且在您提供的链接中没有声明)。在他们拥有的详细示例中,用户提供了调用 __init__ 的代码并提供了我们在此处没有看到的参数,因此当您的 # this works 评论相关时,我不知道您的对象处于什么状态。跨度>
  • 您是否要求查看创建实例的行?
  • 是的,我试图启动您的示例以对其进行测试,但我不确定您是如何实例化它的。
  • 代码已更改。我现在在没有额外参数的情况下创建它。 cherrypy.quickstart(Root(), '/', conf).

标签: python json python-requests cherrypy


【解决方案1】:

原来我错过了标题信息。以下作品:

url = "http://localhost:8080"
data = {'sender': 'Alice', 'receiver': 'Bob', 'message': 'We did it!'}
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
r = requests.post(url, data=json.dumps(data), headers=headers)

【讨论】:

  • 好消息 - 我在 GET 中看到了您的 application/json 并且不知何故错过了您没有在请求中提供它。您可能还需要确保从 POST 返回某些内容,或者您​​可能会收到 500
  • 似乎没有必要。当我打印r 时,我得到<Response [200]>
  • 如何在服务器端检索这个 json?
  • r = requests.get('localhost:8080') c = r.content 结果 = simplejson.loads(c)
  • 在此处使用json.dumps 之前请注意。 requestsdata 参数适用于字典。无需转换为字符串。
【解决方案2】:

从 Requests 版本 2.4.2 开始,您可以在调用中使用 json= parameter(接受字典)而不是 data=(接受字符串):

>>> import requests
>>> r = requests.post('http://httpbin.org/post', json={"key": "value"})
>>> r.status_code
200
>>> r.json()
{'args': {},
 'data': '{"key": "value"}',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Connection': 'close',
             'Content-Length': '16',
             'Content-Type': 'application/json',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.4.3 CPython/3.4.0',
             'X-Request-Id': 'xx-xx-xx'},
 'json': {'key': 'value'},
 'origin': 'x.x.x.x',
 'url': 'http://httpbin.org/post'}

【讨论】:

  • 将此设置为接受的答案,因为从 2.4.2 开始,这更符合习惯。请记住,对于疯狂的 unicode,这可能行不通。
  • 我看到了一个这样的例子,它获取了 dict 对象并在发送之前执行了 json.dumps(object) 。不要这样做......它会弄乱你的 JSON。以上是完美的..你可以传递一个python对象,它变成完美的json。
  • @MydKnight 查看源代码,Requests 设置内容类型,然后执行json.dumps(your_json) 并将结果从str 转换为bytes
【解决方案3】:

从请求 2.4.2 (https://pypi.python.org/pypi/requests) 开始,支持“json”参数。无需指定“内容类型”。所以较短的版本:

requests.post('http://httpbin.org/post', json={'test': 'cheers'})

【讨论】:

    【解决方案4】:

    与 python 3.5+ 完美配合

    客户:

    import requests
    data = {'sender':   'Alice',
        'receiver': 'Bob',
        'message':  'We did it!'}
    r = requests.post("http://localhost:8080", json={'json_payload': data})
    

    服务器:

    class Root(object):
    
        def __init__(self, content):
            self.content = content
            print self.content  # this works
    
        exposed = True
    
        def GET(self):
            cherrypy.response.headers['Content-Type'] = 'application/json'
            return simplejson.dumps(self.content)
    
        @cherrypy.tools.json_in()
        @cherrypy.tools.json_out()
        def POST(self):
            self.content = cherrypy.request.json
            return {'status': 'success', 'message': 'updated'}
    

    【讨论】:

      【解决方案5】:

      更好的办法是:

      url = "http://xxx.xxxx.xx"
      data = {
          "cardno": "6248889874650987",
          "systemIdentify": "s08",
          "sourceChannel": 12
      }
      resp = requests.post(url, json=data)
      

      【讨论】:

        【解决方案6】:

        需要使用data/json/files之间的哪个参数取决于名为Content-Type的请求标头(您可以通过浏览器的开发者工具查看)。

        Content-Typeapplication/x-www-form-urlencoded时,使用data=

        requests.post(url, data=json_obj)
        

        Content-Typeapplication/json 时,您可以只使用json= 或使用data= 并自己设置Content-Type

        requests.post(url, json=json_obj)
        
        requests.post(url, data=jsonstr, headers={"Content-Type":"application/json"})
        

        Content-Typemultipart/form-data时,用于上传文件,所以使用files=

        requests.post(url, files=xxxx)
        

        【讨论】:

        • +1。如果您使用curlify 查看从响应发出的请求,除非您按照这些说明操作,否则不会设置内容类型标头。 print(curlify.to_curl(project.request))
        【解决方案7】:

        始终建议我们需要具备读取 JSON 文件并将对象解析为请求正文的能力。我们不会解析请求中的原始数据,因此以下方法将帮助您解决它。

        def POST_request():
            with open("FILE PATH", "r") as data:
                JSON_Body = data.read()
            response = requests.post(url="URL", data=JSON_Body)
            assert response.status_code == 200
        

        【讨论】:

          【解决方案8】:

          我是这样解决的:

          from flask import Flask, request
          from flask_restful import Resource, Api
          
          
          req = request.json
          if not req :
              req = request.form
          req['value']
          

          【讨论】:

            【解决方案9】:
            headers = {"charset": "utf-8", "Content-Type": "application/json"}
            url = 'http://localhost:PORT_NUM/FILE.php'
            
            r = requests.post(url, json=YOUR_JSON_DATA, headers=headers)
            print(r.text)
            

            【讨论】:

              猜你喜欢
              • 2023-03-23
              • 2017-06-17
              • 2014-06-01
              • 1970-01-01
              相关资源
              最近更新 更多