import requests
url = 'https://...'
payload = {'key1': 'value1', 'key2': 'value2'}
# GET
r = requests.get(url)
# GET with params in URL
r = requests.get(url, params=payload)
# POST with form-encoded data
r = requests.post(url, data=payload)
# POST with JSON
import json
r = requests.post(url, data=json.dumps(payload))
# Response, status etc
r.text
r.status_code
import json
post_response = requests.post(url='http://httpbin.org/post', data=json.dumps(post_data))
# If using requests v2.4.2 or later, pass the dict via the json parameter and it will be encoded directly:
post_response = requests.post(url='http://httpbin.org/post', json=post_data)
请求
https://github.com/kennethreitz/requests/
以下是一些常用的使用方法:
httplib2
https://github.com/jcgregorio/httplib2
你可以用这个来包装urllib2:
它将返回具有结果数据和响应代码的Request对象。
更简单:通过requests模块。
若要发送非形式编码的数据,请将其序列化为字符串(示例取自documentation):
相关问题 更多 >
编程相关推荐