[英]How do I send a POST request as a JSON?
data = {
'ids': [12, 3, 4, 5, 6 , ...]
}
urllib2.urlopen("http://abc.example/api/posts/create",urllib.urlencode(data))
我想发送一个 POST 请求,但其中一个字段应该是数字列表。 我怎样才能做到这一点? (JSON?)
如果您的服务器期望 POST 请求是 json,那么您需要添加一个标头,并为您的请求序列化数据......
Python 2.x
import json
import urllib2
data = {
'ids': [12, 3, 4, 5, 6]
}
req = urllib2.Request('http://example.com/api/posts/create')
req.add_header('Content-Type', 'application/json')
response = urllib2.urlopen(req, json.dumps(data))
Python 3.x
https://stackoverflow.com/a/26876308/496445
如果您不指定标头,它将是默认的application/x-www-form-urlencoded
类型。
我推荐使用难以置信的requests
模块。
http://docs.python-requests.org/en/v0.10.7/user/quickstart/#custom-headers
url = 'https://api.github.com/some/endpoint'
payload = {'some': 'data'}
headers = {'content-type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
对于 python 3.4.2,我发现以下将起作用:
import urllib.request
import json
body = {'ids': [12, 14, 50]}
myurl = "http://www.testmycode.example"
req = urllib.request.Request(myurl)
req.add_header('Content-Type', 'application/json; charset=utf-8')
jsondata = json.dumps(body)
jsondataasbytes = jsondata.encode('utf-8') # needs to be bytes
req.add_header('Content-Length', len(jsondataasbytes))
response = urllib.request.urlopen(req, jsondataasbytes)
这适用于Python 3.5
,如果 URL 包含查询字符串/参数值,
请求 URL = https://bah2.example/ws/rest/v1/concept/
参数值 = 21f6bb43-98a1-419d-8f0c-8133669e40ca
import requests
url = 'https://bahbah2.example/ws/rest/v1/concept/21f6bb43-98a1-419d-8f0c-8133669e40ca'
data = {"name": "Value"}
r = requests.post(url, auth=('username', 'password'), json=data)
print(r.status_code)
这是一个如何使用 Python 标准库中的 urllib.request 对象的示例。
import urllib.request
import json
from pprint import pprint
url = "https://app.close.com/hackwithus/3d63efa04a08a9e0/"
values = {
"first_name": "Vlad",
"last_name": "Bezden",
"urls": [
"https://twitter.com/VladBezden",
"https://github.com/vlad-bezden",
],
}
headers = {
"Content-Type": "application/json",
"Accept": "application/json",
}
data = json.dumps(values).encode("utf-8")
pprint(data)
try:
req = urllib.request.Request(url, data, headers)
with urllib.request.urlopen(req) as f:
res = f.read()
pprint(res.decode())
except Exception as e:
pprint(e)
您必须添加标头,否则您将收到 http 400 错误。 该代码在python2.6,centos5.4上运行良好
代码:
import urllib2,json
url = 'http://www.google.com/someservice'
postdata = {'key':'value'}
req = urllib2.Request(url)
req.add_header('Content-Type','application/json')
data = json.dumps(postdata)
response = urllib2.urlopen(req,data)
在最新的 requests 包中,您可以使用requests.post()
方法中的json
参数发送一个 json dict,并且 header 中的Content-Type
将设置为application/json
。 无需明确指定标头。
import requests
payload = {'key': 'value'}
requests.post(url, json=payload)
这个对我来说很好用 apis
import requests
data={'Id':id ,'name': name}
r = requests.post( url = 'https://apiurllink', data = data)
此处许多答案中使用的Requests包很棒,但不是必需的。 您可以使用 Python 3 标准库在一个步骤中简洁地执行 JSON 数据的 POST:
import json
from urllib import request
request.urlopen(request.Request(
'https://example.com/url',
headers={'Content-Type': 'application/json'},
data=json.dumps({
'pi': 3.14159
}).encode()
))
如果您需要读取结果,您可以从返回的类文件对象中 .read .read()
) 并使用json.loads()
来解码 JSON 响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.