You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Rest Assured发送POST请求遇400断言错误及toJSONString方法缺失问题求助

解决Rest Assured POST请求400错误及JSON序列化问题

Hey there! Let's tackle your two main issues one by one: the 400 Bad Request error and the missing toJSONString method.

1. 为什么会出现400 Bad Request错误?

你的核心问题是请求参数不匹配目标接口的要求:
你调用的https://reqres.in/api/register接口,需要的请求体参数是email和password,而不是你传入的name和job(name/job是/api/users接口的创建用户参数)。服务器收到不符合要求的参数,自然返回400错误。

2. 找不到toJSONString方法的原因

你提到的toJSONString方法并不是所有JSON库都提供:

  • 如果你用的是org.json库的JSONObject类,它没有这个方法,你可以直接用toString()方法来生成合法的JSON字符串。
  • 如果你想用类似的方法,Gson库提供的是toJson()方法(注意大小写),需要通过Gson实例来调用。

另外,Rest Assured其实可以自动帮你序列化Map或Java对象为JSON,不需要手动调用序列化方法,这会简化你的代码。

修正后的测试代码

方案1:使用org.json的JSONObject(修正参数+正确序列化)

import org.json.JSONObject;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;

@Test
public void UserRegistrationSuccessful2() {
    // 改用register接口要求的email和password参数
    JSONObject request = new JSONObject();
    request.put("email", "eve.holt@reqres.in"); // reqres.in提供的测试邮箱
    request.put("password", "pistol"); // 测试密码

    given()
        .header("Content-Type", "application/json")
        .contentType(JSON)
        .accept(JSON)
        .body(request.toString()) // 使用JSONObject的toString()生成JSON
        .when()
        .post("https://reqres.in/api/register")
        .then()
        .statusCode(201);
}

方案2:直接用Map,让Rest Assured自动序列化(更简洁)

Rest Assured会自动把Map转换成JSON,不需要手动处理:

import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;

@Test
public void UserRegistrationSuccessful2() {
    Map<String, Object> map = new HashMap<>();
    map.put("email", "eve.holt@reqres.in");
    map.put("password", "pistol");

    given()
        .contentType(JSON)
        .accept(JSON)
        .body(map) // 直接传Map,Rest Assured自动序列化
        .when()
        .post("https://reqres.in/api/register")
        .then()
        .statusCode(201);
}

方案3:使用Gson库的toJson方法

如果你想用Gson来序列化,代码如下:

import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
import org.testng.annotations.Test;
import static io.restassured.RestAssured.given;
import static io.restassured.http.ContentType.JSON;

@Test
public void UserRegistrationSuccessful2() {
    Map<String, Object> map = new HashMap<>();
    map.put("email", "eve.holt@reqres.in");
    map.put("password", "pistol");

    Gson gson = new Gson();
    String jsonBody = gson.toJson(map); // Gson的toJson方法,不是toJSONString

    given()
        .contentType(JSON)
        .accept(JSON)
        .body(jsonBody)
        .when()
        .post("https://reqres.in/api/register")
        .then()
        .statusCode(201);
}

额外提示

  • 调用第三方接口前,一定要先确认接口的参数要求(reqres.in的接口规则里明确说明了各个接口的必填参数)。
  • Rest Assured的contentType和accept可以用枚举类ContentType.JSON代替字符串,更不容易出错。

内容的提问来源于stack exchange,提问作者Avdhut Joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 15:08:13