0
点赞
收藏
分享

微信扫一扫

requests --- 不同请求方法/请求数据时参数的不同

get请求的参数都在url里或者使用params,post的请求相对于get请求多了个body部分。

使用get请求方法时,headers请求头中的字段没有Content-type,而用post请求方法时headers请求头中存在Content-type

 

一、Python使用requests库发送post请求

我们在使用postman进行接口测试的时候,发现POST请求方法中请求数据的媒体类型有四种,如下:

①application/x-www-form-urlencoded     ==最常见的post提交数据的方式,以form表单形式提交数据

②application/json                   ==以json格式提交数据

③multipart/form-data             ==一般使用来上传文件(较少用)

④text/xml                  == 较少使用

1.第一种:application/json:

这是最常见的 json 格式,如{"input1":"xxx","input2":"ooo","remember":false}

requests --- 不同请求方法/请求数据时参数的不同_get请求

2.第二种:application/x-www-form-urlencoded:

浏览器的原生 form 表单,如果不设置enctype 属性,那么最终就会以 application/x-www-form-urlencoded 方式提交数据,如:input1=xxx&input2=ooo&remember=false

requests --- 不同请求方法/请求数据时参数的不同_xml_02

3.第三种:multipart/form-data:

这一种是表单格式的,数据类型如下 --- 目前未见到

------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="text"
title
------WebKitFormBoundaryrGKCBY7qhFd3TrwA
Content-Disposition: form-data; name="file"; filename="chrome.png"
Content-Type: image/png
PNG ... content of chrome.png ...
------WebKitFormBoundaryrGKCBY7qhFd3TrwA--

requests --- 不同请求方法/请求数据时参数的不同_get请求_03

4.第四种:text/xml:这种直接传的 xml 格式  
<!--?xml version="1.0"?-->
<methodcall>
<methodname>examples.getStateName</methodname>
<params>
<param>
<value><i4>41</i4></value>
</params>
</methodcall>

例如,首先要确定post请求的body部分类型是xml格式,这里我是用fiddler抓包工具来确定的

requests --- 不同请求方法/请求数据时参数的不同_get请求_04

 

 requests --- 不同请求方法/请求数据时参数的不同_提交数据_05

二、Python使用requests发送 Get 请求

get请求方法相对来说简单一点,主要分为两种:

  A:不带参数的请求

  B:带参数去的请求

A: Requests 发送 不带参数Get请求,具体格式如下:

requests --- 不同请求方法/请求数据时参数的不同_xml_06

 B: Requests 发送 带参数Get请求,具体格式如下:

requests --- 不同请求方法/请求数据时参数的不同_get请求_07

 

 requests --- 不同请求方法/请求数据时参数的不同_xml_08

 

 requests --- 不同请求方法/请求数据时参数的不同_提交数据_09

 

去期待陌生,去拥抱惊喜。

举报

相关推荐

0 条评论