前言

发送post的请求参考例子很简单,实际遇到的情况却是很复杂的,首先第一个post请求肯定是登录了,但登录是最难处理的。登录问题解决了,后面都简单了。

一、查看官方文档

1.学习一个新的模块,其实不用去百度什么的,直接用help函数就能查看相关注释和案例内容。

>>import requests

>>help(requests)

2.查看python发送get和post请求的案例

>>> import requests
       >>> r = requests.get('https://www.python.org')
       >>> r.status_code
       200
       >>> 'Python is a programming language' in r.content
       True
    
    ... or POST:
    
       >>> payload = dict(key1='value1', key2='value2')
       >>> r = requests.post('http://httpbin.org/post', data=payload)
       >>> print(r.text)
       {
         ...
         "form": {
           "key2": "value2",
           "key1": "value1"
         },
         ...
       }

二,发送post请求

  ♦1.用上面给的案例,做个简单修改,发个post请求

  ♦2.payload参数是字典类型,传到如下图的form里

实例代码:

import requests#导入request模块
import json
url = 'https://httpbin.org/post'
payload= {"pw":'1011634093@qq.com',"un":"password"}#值以字典的形式传入
response = requests.post(url=url,data=payload)
print(response.text)

运行结果:

F:\Python\python.exe F:/Python/Interface_automation/post_requests.py
{"args":{},"data":"","files":{},
"form":{"pw":"1011634093@qq.com","un":"password"},#
  "headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate",
  "Connection":"close","Content-Length":"",
  "Content-Type":"application/x-www-form-urlencoded",
  "Host":"httpbin.org","User-Agent":"python-requests/2.18.4"}, "json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"} Process finished with exit code

三、json

  ♦1.post的body是json类型,也可以用json参数传入。

  ♦2.先导入json模块,用dumps方法转化成json格式。

  ♦3.返回结果,传到data里

实例代码如下:

import requests#导入request模块
import json#导入json模块
url = 'https://httpbin.org/post'body= {"pw":'1011634093@qq.com',"un":"password"}
data_json = json.dumps(body)#转化成json类型
response = requests.post(url=url,data=data_json,)
print(response.status_code)
print(response.text)

运行结果:

F:\Python\python.exe F:/Python/Interface_automation/post_requests.py

b'{"args":{},
"data":"{\\"pw\\": \\"1011634093@qq.com\\",
\\"un\\": \\"password\\"}","files":{},"form":{},
"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate",
"Connection":"close","Content-Length":"45","Host":"httpbin.org",
"User-Agent":"python-requests/2.18.4"},"json":{"pw":"1011634093@qq.com","un":"password"},#json类型
"origin":"61.175.197.202","url":"https://httpbin.org/post"}\n' Process finished with exit code

四、headers

  ♦4.1.我们在请求数据时也可以加上自定义的headers(通过headers关键字参数传递)有时候有的特殊的请求必须加上headers头信息:

实例代码:

import requests#导入request模块
import json
url = 'https://httpbin.org/post'
headers = {"Connection":'keep-alive',"Host":"httpbin.org"}#值以字典的形式传入
response = requests.post(url=url,headers=headers)#用关键字headers传入
print(response.text)

数据结果:

F:\Python\python.exe F:/Python/Interface_automation/post_requests.py
{"args":{},"data":"","files":{},"form":{},
"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},
"json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"}
Process finished with exit code

  ♦4.2.headers的取出方法:

我们有一下一种取出方法:

print(response.headers)#打印出响应头
print(response.headers['Connection'])#取得部分响应头以做判断:
print(response.headers.get('Connection'))#或者这样也可以

代码实例:

import requests#导入request模块
import json
url = 'https://httpbin.org/post'
headers = {"Connection":'keep-alive',"Host":"httpbin.org"}#值以字典的形式传入
response = requests.post(url=url,headers=headers)#用关键字headers传入
print(response.headers)#打印出响应头
print(response.headers['Connection'])#取得部分响应头以做判断:
print(response.headers.get('Connection'))#或者这样也可以
print(response.text)

输出结果:

F:\Python\python.exe F:/Python/Interface_automation/post_requests.py
{'Connection': 'keep-alive', 'Server': 'gunicorn/19.8.1', 'Date': 'Tue, 29 May 2018 08:30:55 GMT', 'Content-Type': 'application/json', 'Content-Length': '', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'Via': '1.1 vegur'}
keep-alive#print(response.headers['Connection'])#取得部分响应头以做判断:
keep-alive#print(response.headers.get('Connection'))#或者这样也可以
{"args":{},"data":"","files":{},"form":{},"headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Connection":"close","Content-Length":"","Host":"httpbin.org","User-Agent":"python-requests/2.18.4"},"json":null,"origin":"61.175.197.202","url":"https://httpbin.org/post"} Process finished with exit code

  ♦在我看来不管是get请求还是post请求我们都是发送我们想要发送的数据给服务器,然后查看服务器相应回来的数据看看这些书是否和我们想要的内容是否相符,只是get和post的请求机制不一样,但是所要做的思路是一样的。

后面我会在整理一下requests模块中的其他的东西。

python接口自动化2-发送post请求详解(二)的更多相关文章

  1. python接口自动化26-发xml格式post请求《转载》

    python接口自动化26-发xml格式post请求 https://cloud.tencent.com/developer/article/1164987

  2. python接口自动化1-发送get请求

    前言 requests模块,也就是老污龟,为啥叫它老污龟呢,因为这个官网上的logo就是这只污龟,接下来就是学习它了. 一.环境安装 1.用pip安装requests模块 >>pip in ...

  3. python接口自动化25-发xml格式post请求

    前言 post请求相对于get请求多一个body部分,body部分常见的数据类型有以下四种(注意是常见的,并不是只有4种) application/x-www-form-urlencoded appl ...

  4. python接口自动化之发送get(三)

    1.安装requests requests是python的第三方库,需要进行安装.安装之前最好先关闭fiddler cmd(win+R快捷键)输入:pip install requests 其他命令: ...

  5. python接口自动化发送get请求 详解(一)

    前言:接口自动化实现自动化脚本比较稳定,主要用到requests模块,后面我会把这个模块单独拉出来写一下. 一.环境安装 1.用pip安装requests模块 >>pip install ...

  6. python+pytest接口自动化(5)-发送post请求

    简介 在HTTP协议中,与get请求把请求参数直接放在url中不同,post请求的请求数据需通过消息主体(request body)中传递. 且协议中并没有规定post请求的请求数据必须使用什么样的编 ...

  7. python接口自动化2-发送post请求

    发送post的请求参考例子很简单,实际遇到的情况却是很复杂的,首先第一个post请求肯定是登录了,但登录是最难处理的.登录问题解决了,后面都简单了. 一.查看官方文档 1.学习一个新的模块,其实不用去 ...

  8. 跨域发送HTTP请求详解

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客讲述几种跨域发HTTP请求的几种方法,POST请求,GET请求 目录: 一,采用JsonP的方式(只能 ...

  9. python接口自动化1-发送get请求【转载】

    本篇转自博客:上海-悠悠 原文地址:http://www.cnblogs.com/yoyoketang/tag/python%E6%8E%A5%E5%8F%A3%E8%87%AA%E5%8A%A8%E ...

随机推荐

  1. Information Retrieval II

    [Information Retrieval II] 搜索引擎分类: 1.目录式搜索引擎. 2.全文搜索引擎. 3.元搜索引擎(Meta-Search Engine). 搜索引擎的4个阶段:下载(cr ...

  2. 网页设计编辑利器——jQuery EasyUI所学整理(待编辑)

    1, Messager弹窗信息 方法: $.messager.alert(...), 在网页中间弹出一个窗口 $.messager.confirm(...) 弹出一个确认窗口, 有确定和取消两个按钮, ...

  3. 使用ES(elasticsearch) 搜索引擎

    介绍  https://blog.csdn.net/andyzhaojianhui/article/details/75195296 创建语句 { "company":{ &quo ...

  4. mybatis使用foreach进行批量插入和删除操作

    一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...

  5. Emacs中编辑保存makefile文件时会错误地将TAB转成空格的解决方法

    问题描述 我的Emacs使用了Purcell的配置,在其配置中使用了whitespace-cleanup,且通过在.emacs.d/lisp/init-edit-utils.el中设定: (requi ...

  6. dnn ubuntu 问题

    http://blog.csdn.net/moshuilangting/article/details/53926622 http://blog.csdn.net/enjoyyl/article/de ...

  7. Mac下eclipse 启动时出现An error has occurred. See the log file的问题

    eclipse原来可以使用的好好的,装了多个版本的jdk后,打开eclipse出现An error has occurred. See the log file的问题,经过查找,可能原因之一是机子装了 ...

  8. 转 org.aspectj.lang.JoinPoint-中文简要API

    AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...

  9. (DP)uva 10036 Problem C: Divisibility

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88171#problem/F 代码: #include <cstdio> ...

  10. UVALive - 6436 —(DFS+思维)

    题意:n个点连成的生成树(n个点,n-1条边,点与点之间都连通),如果某个点在两点之间的路径上,那这个点的繁荣度就+1,问你在所有点中,最大繁荣度是多少?就比如上面的图中的C点,在A-B,A-D,A- ...