使用rest-assured得到500,但使用Postman得到200

问题描述 投票:0回答:1

我是邮递员,我输入 URL、POST 方法、标头(键 - x-api-key 和值)、正文(listId 和值)并通过发送请求获得 200 成功响应。

但是使用maven项目,java语言,放心,我得到了-

响应状态码:500 & 响应正文:由于发生内部服务器错误,页面无法显示。

这是我的代码-

import org.testng.Assert;
import org.testng.annotations.Test;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Method;
import io.restassured.response.Response;
import io.restassured.response.ResponseBody;
import io.restassured.specification.RequestSpecification;

import static io.restassured.RestAssured.given;

public class TC001_CacheStatus {
    @Test
    public void CacheStatusCheck() throws IOException {
     
        // Set base URI of the API
        String baseUrl = "https://url.here";

        // Define headers
        String apiKey = "x-api-key";
        String apiValue = "value_here";

        // Define form data
        String formDataKey = "listId";
        String listId = "65cd9b8f";

        // Send POST request with headers and form data
        Response response =   RestAssured.given()
                                    .baseUri(baseUrl)
                                    .header("x-api-key", apiKey) 
                                    .multiPart("listId", listId)
                                    .contentType("multipart/form-data")
                                    .post("/endpoint")
                                    .andReturn();


        // Print response
        System.out.println("Response status code: " + response.getStatusCode());
        System.out.println("Response body: " + response.getBody().asString());

    }
}

我该如何解决这个问题?请帮忙。

java api rest-assured
1个回答
0
投票

尝试“response.then().log().ifValidationFails()”来检查后查询是否正确。

© www.soinside.com 2019 - 2024. All rights reserved.