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())
去期待陌生,去拥抱惊喜。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!