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

Rest Assured API出现Assertion Error问题求助

修复Rest Assured脚本中GET请求404错误的解决方案

问题根源

你的脚本中存在请求参数key的拼写错误:

  • POST请求使用了正确的key值:qaclick123
  • PUT请求的key写成了qaclik123(缺少字母c)
  • GET请求的key写成了qackick123(完全拼写错误)

尽管PUT请求返回了200状态码(可能是接口的容错处理),但GET请求对key的校验更严格,无效的key导致系统无法识别对应的place_id,最终返回404错误。

修复后的完整代码

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import Files.Payload;

public class Basics {
    public static void main(String[] args) {
        RestAssured.baseURI = "https://rahulshettyacademy.com";
        
        // 新增地点
        String response = given().log().all()
                .queryParam("key", "qaclick123")
                .header("Content-Type", "application/json")
                .body(Payload.AddPlace())
                .when().post("maps/api/place/add/json")
                .then().assertThat()
                .statusCode(200)
                .body("scope", equalTo("APP"))
                .header("Server", "Apache/2.4.52 (Ubuntu)")
                .extract().response().asString();
        
        System.out.println(response);
        JsonPath js = new JsonPath(response);
        String PlaceId = js.getString("place_id");
        System.out.println(PlaceId);
        
        // 更新地点
        String newAddress = "Summerwalk, Africa";
        given().log().all()
                .queryParam("key", "qaclick123") // 修复key拼写错误
                .header("Content-Type", "application/json")
                .body("{\r\n"
                        + "\"place_id\":\""+PlaceId+"\",\r\n"
                        + "\"address\":\""+newAddress+"\",\r\n"
                        + "\"key\":\"qaclick123\"\r\n"
                        + "}")
                .when().put("maps/api/place/update/json")
                .then().assertThat().log().all()
                .statusCode(200)
                .body("msg", equalTo("Address successfully updated"));
        
        // 获取地点信息
        String getPlaceResponse = given().log().all()
                .queryParam("key", "qaclick123") // 修复key拼写错误
                .queryParam("place_id", PlaceId)
                .when().get("maps/api/place/get/json")
                .then().assertThat().log().all()
                .statusCode(200)
                .extract().response().asString();
        
        JsonPath js1 = new JsonPath(getPlaceResponse);
        String actualAddress = js1.getString("address");
        System.out.println(actualAddress);
    }   
}

关键修复点

  1. 将PUT请求中的queryParam("key", "qaclik123")修正为queryParam("key", "qaclick123")
  2. 将GET请求中的queryParam("key", "qackick123")修正为queryParam("key", "qaclick123")

修复后,GET请求将使用正确的key参数,系统能够正常识别place_id,返回200状态码及正确的地址信息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 14:12:03