我想使用urllib3库通过requests库发出POST请求,因为它具有连接池和重试等功能。但是我找不到任何替代以下POST请求的方法。
import requests
result = requests.post("http://myhost:8000/api/v1/edges", json={'node_id1':"VLTTKeV-ixhcGgq53", 'node_id2':"VLTTKeV-ixhcGgq51", 'type': 1 })这在requests库中工作得很好,但我不能将其转换为urllib3请求。我试过了
import json
import urllib3
urllib3.PoolManager().request("POST","http://myhost:8000/api/v1/edges", body=json.dumps(dict(json={'node_id1':"VLTTKeV-ixhcGgq53", 'node_id2':"VLTTKeV-ixhcGgq51", 'type': 1 })))问题是在POST请求中使用json作为关键字传递原始json数据。
发布于 2015-08-30 19:55:12
您不需要json关键字参数;您正在将字典包装在另一个字典中。
您还需要添加一个Content-Type标头,将其设置为application/json
http = urllib3.PoolManager()
data = {'node_id1': "VLTTKeV-ixhcGgq53", 'node_id2': "VLTTKeV-ixhcGgq51", 'type': 1})
r = http.request(
"POST", "http://myhost:8000/api/v1/edges",
body=json.dumps(data),
headers={'Content-Type': 'application/json'})https://stackoverflow.com/questions/32296255
复制相似问题