软件测试 |From请求

113 阅读1分钟

1.From请求简介

From请求在请求过程中请求体为表单类型。其特点为:对于数据量不大且数据层级不深的表单,可以使用键值对传递参数。From请求头中的content-type通常对应application/x- www-from-urlencoded。

2.实战演示

实战演示代码如下(Python版和Java版)。

在Python编程实现中,我们可以使用data参数传输表单数据,data参数以字典的形式表示,字段是以键值对的形式出现。

class TestFormData:
     def test_data(self):
          data = {
              "school":"hogwarts"
          }
          r = requests.post("https://httpbin.ceshiren.com/post",
                            data=data)
           print(r.json)

运行结果:

{
  "args":{},
  "data":""
  "files":{},
  "form":{
     "school":"hogwarts"
  },
  //省略
  "json":null,
  "origin":"113.89.10.187",
  "url":"https://httpbin.ceshiren.com/post"
}

(2)Java演示代码

import static io.restassured.RestAssured.*;
public class Requests {
    public static void main (String[] args) {
        given().formParams("school","hogwarts").when().post("https://
httpbin.ceshiren.com/post").
              then().log().all();
    }
}

使用抓包工具Charles查看接口参数传递的数据,如图7-2所示,From请求显示的结果中多了From格式的信息。From格式的信息以Name和Value的字段显示。

image-20230214152523383.png