requests--传递参数
传递参数
传递URL参数
data = {'city': '北京'}
# 参数有中文如果发送不了,必须要编码
city = parse.urlencode(data).encode('utf-8')
一般在GET请求中使用查询字符串(query string)来进行参数传递,在requests库中使用如下方法:
import requests base_url = 'http://httpbin.org'
# 将参数存在字典里
params_data = {"user": "zou", "pwd": ''} r = requests.get(base_url + '/get', params=params_data)
print(r.url) # 打印URL
print(r.status_code)
结果
http://httpbin.org/get?user=zou&pwd=31500
200
传递body参数
在post请求里有两个参数,data为form表单格式的,json为Content-Type是json格式的。返回的值如果是json格式的,可以用r.json(),r.text不管是json格式还是html格式的都可以
在POST请求中,一般参数都在请求体中传递,在requests中用法如下:
import requests base_url = 'http://httpbin.org'
# 将参数存在字典里
form_data = {"user": "zou", "pwd": ''} r = requests.post(base_url + '/post', data=form_data)
print(r.url) # 打印URL
print(r.status_code)
print(r.text) # 打印出响应文本
结果:
http://httpbin.org/post
200
{
"args": {},
"data": "",
"files": {},
"form": {
"pwd": "",
"user": "zou"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "112.10.81.210, 112.10.81.210",
"url": "https://httpbin.org/post"
}
data和json的区别
如果我们发送post请求时,里面有个参数json,还有个参数时data,它们都是提交的服务器的数据,那它们有什么区别呢?
data与json既可以是str,也可以是dict
区别如下:
不管json是str还是dict,如果不指定headers中的content-type,默认为application/json
data为dict时,如果不指定content-type,默认为application/x-www-form-urlencoded,相当于普通form表单提交的形式,此时数据可以从request.POST里面获取,而request.body的内容则为a=1&b=2的这种形式,注意,即使指定content-type=application/json,request.body的值也是类似于a=1&b=2,所以并不能用json.loads(request.body.decode())得到想要的值
data为str时,如果不指定content-type,默认为application/json
原始响应内容
可以找出 Requests 使用了什么编码,并且能够使用r.encoding 属性来改变它:
r.encoding # 获取编码格式
'utf-8' r.encoding = 'ISO-8859-1' # 设置编码格式
在罕见的情况下,你可能想获取来自服务器的原始套接字响应,那么你可以访问 r.raw。 如果你确实想这么干,那请你确保在初始请求中设置了 stream=True。具体你可以这么做:
>>> r = requests.get('https://api.github.com/events', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
如果发送了一个错误请求(一个 4XX 客户端错误,或者 5XX 服务器错误响应),我们可以通过Response.raise_for_status() 来抛出异常:
>>> bad_r = requests.get('http://httpbin.org/status/404')
>>> bad_r.status_code
404
>>> bad_r.raise_for_status()
Traceback (most recent call last):
File "requests/models.py", line 832, in raise_for_status
raise http_error
requests.exceptions.HTTPError: 404 Client Error
requests--传递参数的更多相关文章
- requestS模块发送请求的时候怎么传递参数
首先要确定接口的传递参数是什么类型的,如果接口是查询,使用get请求方法,传递参数的时候使用params, 如果接口需要的json型参数的话,使用json,如果是上传文件的话,通过files参数在传递 ...
- Python threadpool传递参数
threadpool模块是一个很老的实现python线程池的模块,pypi已经建议用multiprocessing代替它了,但是,它使用的便捷性还是征服了一批忠实用户. threadpool模块实现多 ...
- linux内核可以接受的参数 | Linux kernel启动参数 | 通过grub给内核传递参数
在Linux中,给kernel传递参数以控制其行为总共有三种方法: 1.build kernel之时的各个configuration选项. 2.当kernel启动之时,可以参数在kernel被GRUB ...
- Vue 给子组件传递参数
Vue 给子组件传递参数 首先看个例子吧 原文 html <div class="container" id="app"> <div clas ...
- [转] C++的引用传递、指针传递参数在java中的相应处理方法
原文出处:[http://blog.csdn.net/conowen/article/details/7420533] 首先要明白一点,java是没有指针这个概念的. 但是要实现C++的引用传递.指针 ...
- 记一次WinForm程序中主进程打开子进程并传递参数的操作过程(进程间传递参数)
目标:想在WinForm程序之间传递参数.以便子进程作出相应的处理. 一种错误的方法 父进程的主程序: ProcessStartInfo psi = new ProcessStartInfo(); p ...
- 在 Angularjs 中 ui-sref 和 $state.go 如何传递参数
1 ui-sref.$state.go 的区别 ui-sref 一般使用在 <a>...</a>: <a ui-sref="message-list" ...
- Linux线程体传递参数的方法详解
传递参数的两种方法 线程函数只有一个参数的情况:直接定义一个变量通过应用传给线程函数. 例子 #include #include using namespace std; pthread_t thre ...
- 【hadoop】如何向map和reduce脚本传递参数,加载文件和目录
本文主要讲解三个问题: 1 使用Java编写MapReduce程序时,如何向map.reduce函数传递参数. 2 使用Streaming编写MapReduce程序(C/C++ ...
- python 函数传递参数的多种方法
python中函数根据是否有返回值可以分为四种:无参数无返回值,无参数有返回值,有参数无返回值,有参数有返回值. Python中函数传递参数的形式主要有以下五种,分别为位置传递,关键字传递,默认值传递 ...
随机推荐
- 转载-Qualcomm MSM8953启动流程:PBL-SBL1-(bootloader)LK-Android
文章转载链接: https://blog.csdn.net/RadianceBlau/article/details/73229005 对于嵌入式工程师了解芯片启动过程是十分有必要的,在分析.调试各种 ...
- Course: ISA 414
Assignment #4Course: ISA 414Points:100Due date: November 18th, 2019, before 11:59 pmSubmission instr ...
- Spring自动注入,类型注入、名称注入(两种方式)
参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...
- Netty中FastThreadLocal源码分析
Netty中使用FastThreadLocal替代JDK中的ThreadLocal[JAVA]ThreadLocal源码分析,其用法和ThreadLocal 一样,只不过从名字FastThreadLo ...
- Ubuntu Err:1 http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not resolve 'us.archive.ubuntu.com' 错误
Ubuntu 更新 apt-get update 的时候 出现 Err: http://us.archive.ubuntu.com/ubuntu bionic InRelease Could not ...
- STorM32 BGC三轴云台控制板电机驱动电路设计(驱动芯片DRV8313)
1 序言 相信对云台有兴趣的小伙伴对STorM32 BGC这块云台控制板并不陌生,虽说这块控制板的软件已经不再开源,但是在GitHub上依旧可以找到两三个版本的代码,而硬件呢我们也可以从Olliw( ...
- Map-HashMap-遍历
第一种遍历方法 : 先获取Map中的所有key值,然后根据key,依次从Map中去数据 (针对只取 Key 或者 Value 的情况) Map<String, String> hashMa ...
- Restful API接口规范
1. 域名 应该尽量将API部署在专用域名之下. https://api.example.com 如果确定API很简单,不会有进一步扩展,可以考虑放在主域名下. https://example.org ...
- Ext学习之路——Ext.application
Ext.application({ name: 'MyApp', launch: function() { Ext.create('Ext.container.Viewport', { items: ...
- java--set,Collections,map
set 特点: 无序, 不允许重复 没有索引 Set<String> set = new HashSet<String>(); set.add("hello" ...