How do I pass raw POST data into urllib3?

后端 未结 1 1569
粉色の甜心
粉色の甜心 2021-01-19 18:29

Trying to use urllib3 to post JSON-encoded data. Just want my POST payload to be raw JSON string, with content type application/json. I just cannot see how to do this.

1条回答
  •  北海茫月
    2021-01-19 18:41

    you can't use PoolManager.request for that, it tries to concoct the body iself, use the lower level urlopen:

    In [16]: pool = urllib3.PoolManager()
    
    In [17]: print pool.urlopen('POST', 'http://httpbin.org/post', headers={'Content-Type':'application/json'}, body='{"sup":"son"}').data
    {
      "data": "{\"sup\":\"son\"}",
      "form": {},
      "json": {
        "sup": "son"
      },
      "origin": "50.74.23.243",
      "args": {},
      "url": "http://httpbin.org/post",
      "files": {},
      "headers": {
        "Host": "httpbin.org",
        "Content-Length": "13",
        "Content-Type": "application/json",
        "Accept-Encoding": "identity",
        "Connection": "close"
      }
    }
    

    0 讨论(0)
提交回复
热议问题