本文转自测试人社区,原文链接: https://ceshiren.com/t/topic/28420

目录

  • form 表单请求简介
  • form 表单的使用

form 表单请求简介

  • application/x-www-form-urlencoded
  • 应用场景
  • 数据量不大
  • 数据层级不深的情况
  • 通常以键值对传递

软件测试学习笔记丨接口请求体-form表单_表单

form 表单请求的使用

  • 调用 formParam() 方法
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.specification.ProxySpecification.host;

public class TestFormParam {

    @Test
    void testFormParam() {

        // 配置本地代理,方便监听请求信息
        RestAssured.proxy = host("localhost").withPort(8888);
        // 忽略HTTPS校验
        RestAssured.useRelaxedHTTPSValidation();

        // 发送 POST 请求
        given()
                .formParam("username", "hogwarts")  // 添加表单数据
                .log().headers()  // 打印请求头信息
                .log().body()  // 打印请求体信息
        .when()
                .post("https://httpbin.ceshiren.com/post")  // 发送请求
        .then()
                .statusCode(200);  // 响应断言
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

form 表单请求的使用

  • 调用 formParams() 方法
import io.restassured.RestAssured;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.specification.ProxySpecification.host;

public class TestFormParams {

    @Test
    void testFormParams() {

        // 配置本地代理,方便监听请求信息
        RestAssured.proxy = host("localhost").withPort(8888);
        // 忽略HTTPS校验
        RestAssured.useRelaxedHTTPSValidation();

        // 发送 POST 请求
        given()
                .formParams("username", "hogwarts",
                 "pwd", "666")  // 添加表单数据
                .log().headers()
                .log().body()
        .when()
                .post("https://httpbin.ceshiren.com/post")  // 发送请求
        .then()
                .statusCode(200);  // 响应断言
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.