前言

发送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. gorm中自己写sql的方法实现

    type Result struct { Total int } var result Result //当天修改作业的总时间:分钟 dao.DB(dao.HomeworkTable).Raw(&qu ...

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

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

  3. Visual Studio工具 vcpkg简介

    博客参考: https://blog.csdn.net/cjmqas/article/details/79282847#43-%E7%A7%BB%E9%99%A4%E5%85%A8%E5%B1%80% ...

  4. USB相关注册表

    计算机\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{ SYSTEM\\CurrentControlSet\\Control\\ ...

  5. 生产消费者队列(TaskCompletionSource)的应用

    using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Li ...

  6. 深入浅出NetWorking

    技术屌丝也是需要学习网络基本知识的,本书用一种轻松的方式讲了最基本的网络概念和硬件.协议,看完此书,就可以学习<TCP/IP协议>,学完后从此网络再无战事了. 1.网线分为:双绞线,同轴电 ...

  7. Undo Architecture

    [Undo Architecture] NSUndoManager is a general-purpose recorder of operations for undo and redo. NSU ...

  8. Bitmap Images and Image Masks

    [Bitmap Images and Image Masks] Bitmap images and image masks are like any drawing primitive in Quar ...

  9. UVa 12093 Protecting Zonk (树形DP)

    题意:给定一个有n个节点的无根树,有两种装置A和B,每种都有无限多个.在某个节点X使用A装置需要C1的花费,并且此时与节点X相连的边都被覆盖.在某个节点X使用B装置需要C2的花费,并且此时与节点X相连 ...

  10. linux 搭建php网站许愿墙

    网站素材在:https://i.cnblogs.com/Files.aspx 首先需要搭建本地yum源,详情参考: http://www.cnblogs.com/jw35/p/5967677.html ...