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

如何在Rest Assured/Java中开启自动跟随重定向解决307问题?

Rest Assured POST请求返回307临时重定向问题解决

问题背景

POST请求在Postman中开启「Automatically follow redirect」可正常运行,关闭则返回307;使用Rest Assured调用时同样返回307,尝试过多种重定向配置但未生效,需基于现有代码修复。

现有代码

static {
    baseURI = ConfigReader.getProperty("url");
}

RequestSpecification requestSpecification;
Response response;
String accessToken;


@Given("I am authorized at endpoint {string}")
public void getAccessToken(String endpoint) {

    response = given().log().all()
            .header("Content-Type", "application/json").body("{\n" +
                    " \"username\": \"" + ConfigReader.getProperty("username") + "\",\n" +
                    "\"password\": \"" + ConfigReader.getProperty("password") + "\" \n" +
                    "}").post(endpoint);
    String jsonString = response.asString();
    accessToken = JsonPath.from(jsonString).get("AccessToken");
}


@When("I add the header {string} {string}")
public void setAuthorizationHeaders(String key, String value) {

    requestSpecification = given()
            .header(key, value).
            header("AccessToken", accessToken);
}


 @Then("I send a POST request to {string} endpoint with the body {body}")
    public void iSendAPOSTRequestToEndpointWithTheBody(String string, String body) throws InterruptedException {

        requestSpecification.body(new File("body"))
                .post(string).then().log().all().
                 statusCode(200);
    }

无效配置尝试

// 错误:设置了不跟随重定向
given().config(RestAssured.config().redirect(redirectConfig().followRedirects(false)))

// 错误:同样设置了不跟随重定向
RequestSpecification spec = new RequestSpecBuilder().setConfig(RestAssured.config().redirect(redirectConfig().followRedirects(false))).build();

// 错误:maxRedirects(0)直接禁用重定向
RestAssured.config = config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));

解决方案

你的无效配置要么明确关闭了重定向,要么通过maxRedirects(0)禁用了重定向,这是问题核心。Rest Assured默认开启自动跟随重定向,只需确保配置正确开启且重定向次数足够。

方式1:全局配置(所有请求生效)

在静态代码块中添加全局重定向配置:

static {
    baseURI = ConfigReader.getProperty("url");
    // 开启自动跟随重定向,设置合理的最大重定向次数
    RestAssured.config = RestAssured.config()
            .redirect(RedirectConfig.redirectConfig()
                    .followRedirects(true)
                    .maxRedirects(10)); // 可根据实际接口调整次数
}

方式2:单个请求配置

仅给需要重定向的请求配置:

@When("I add the header {string} {string}")
public void setAuthorizationHeaders(String key, String value) {
    requestSpecification = given()
            .header(key, value)
            .header("AccessToken", accessToken)
            // 配置当前请求跟随重定向
            .config(RestAssured.config()
                    .redirect(RedirectConfig.redirectConfig()
                            .followRedirects(true)
                            .maxRedirects(10)));
}

额外注意

你的iSendAPOSTRequestToEndpointWithTheBody方法中,body(new File("body"))存在路径问题,应该使用参数body作为文件路径,修改为:

requestSpecification.body(new File(body))
        .post(string).then().log().all().statusCode(200);

否则会尝试读取名为"body"的文件,可能导致请求体错误,间接影响重定向逻辑。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 15:10:26