requests的不同请求方法时的参数请求方式
前言:

测试网址
①http://httpbin.org/get
②http://httpbin.org/post
1、GET请求方法 (带请求参数)
""" URL Parameters 请求方式: URL参数 例如: 以get方式请求 http://httpbin.org/get?first_name=hello&last_name=word """ import requests params = {"first_name": "hello", "last_name": "word"} responds = requests.get("http://httpbin.org/get", params=params) print(responds.json()) # {'args': {'first_name': 'hello', 'last_name': 'word'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.28.0', 'X-Amzn-Trace-Id': 'Root=1-64087bc3-002182b14f5b8b0a6de702ff'}, 'origin': '101.71.248.138', 'url': 'http://httpbin.org/get?first_name=hello&last_name=word'} print(responds.headers) # {'Date': 'Wed, 08 Mar 2023 12:11:28 GMT', 'Content-Type': 'application/json', 'Content-Length': '394', 'Connection': 'keep-alive', 'Server': 'gunicorn/19.9.0', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true'} print(responds.url) # http://httpbin.org/get?first_name=hello&last_name=word
2、POST请求方法 (请求数据的媒体类型为 application/x-www-form-urlencode )
""" 表单参数提交 Content-Type: application/x-www-form-urlencoded 例如: 以post方式请求 http://httpbin.org/post 增加的资源为data={"first_name":"hello","last_name":"word"} """ import requests headers = {"Content-Type": "application/x-www-form-urlencoded"} data = {"first_name": "hello", "last_name": "word"} responds = requests.post("http://httpbin.org/post", data=data, headers=headers) print(responds.request.body) # first_name=hello&last_name=word print(responds.request.url) # http://httpbin.org/post print(responds.json()) # {'args': {}, 'data': '', 'files': {}, 'form': {'first_name': 'hello', 'last_name': 'word'}, 'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '31', 'Content-Type': 'application/x-www-form-urlencoded', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.28.0', 'X-Amzn-Trace-Id': 'Root=1-64087f1b-546687c12315943d31ed342a'}, 'json': None, 'origin': '101.71.248.138', 'url': 'http://httpbin.org/post'}
3、POST请求方法(请求数据的媒体类型为 application/json )
""" Json数据提交 Content-Type: application/json 例如: 以post方式请求 http://httpbin.org/post 增加的资源为data={"first_name":"hello","last_name":"word"} """ import json import requests # 第一种方法 data = {"first_name": "hello", "last_name": "word"} headers = {"Content-Type": "application/json"} responds = requests.post("http://httpbin.org/post", json=data, headers=headers) print(responds.request.body, type(responds.request.body)) # b'{"first_name": "hello", "last_name": "word"}' <class 'bytes'> # 第二种方法 data = {"first_name": "hello", "last_name": "word"} headers = {"Content-Type": "application/json"} responds = requests.post("http://httpbin.org/post", data=json.dumps(data), headers=headers) print(responds.request.body, type(responds.request.body)) # {"first_name": "hello", "last_name": "word"} <class 'str'>
4、POST请求方法(请求数据的媒体类型为 multipart/form-data )
除了传统的application/x-www-form-urlencoded表单,我们另一个经常用到的是上传文件用的表单,这种表单信息的媒体类型为multipart/form-data。【请求信息同时包含文件和表单数据】
①请求信息为表单数据(不包含文件)
requests.post(url='', data={'key1':'value1','key2':'value2'}, headers={'Content-Type':'multipart/form-data'})
②请求信息同时包含表单数据以及文件
""" 发送文件中的数据需要(安装requests_toolbelt): 例如: 以post方式请求 http://httpbin.org/post """ from requests_toolbelt import MultipartEncoder import requests m = MultipartEncoder( fields={ 'field0': 'value', 'field1': 'value', 'field2': ('filename', open('file.py', 'rb'), 'text/plain') } ) r = requests.post('http://httpbin.org/post', data=m, headers={'Content-Type': m.content_type}) print(r.json())
5、POST请求方法 (请求数据的媒体类型为 binary )
""" 例如: 以post方式请求 http://httpbin.org/post Requests库 支持请求信息以multipart媒体类型请求;只需将文件传给requests.post()中的files参数即可 """ import requests response = requests.post(url='http://httpbin.org/post', headers={'Content-Type': 'binary'}, files={'file': open('test.xls', 'rb')}) print(response.json())
去期待陌生,去拥抱惊喜。
浙公网安备 33010602011771号