首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何打开“自动跟随重定向”在Rest /Java中

如何打开“自动跟随重定向”在Rest /Java中
EN

Stack Overflow用户
提问于 2022-11-06 03:42:04
回答 1查看 45关注 0票数 0

我在这个问题上被困住了,请帮我.

所以,邮政要求工作良好的邮递员,但在放心,我得到307临时重定向状态代码。我发现这个问题与重定向有关。因为我检查了邮递员的设置,当我打开“自动跟随重定向”切换时,它在邮递员中也会出现同样的问题,但当它打开时也会工作。

如何在我的代码中添加相同的内容(在中打开“自动跟踪重定向”)?

这是我的代码:

代码语言:javascript
复制
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);
    }

,我已经使用了这些方法,但是它们没有起作用(或者我用错了):

,请根据我的上述代码提出建议!

代码语言:javascript
复制
given().config(RestAssured.config().redirect(redirectConfig().followRedirects(false)))

RequestSpecification spec = new RequestSpecBuilder().setConfig(RestAssured.config().redirect(redirectConfig().followRedirects(false))).build();

RestAssured.config = config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));
EN

回答 1

Stack Overflow用户

发布于 2022-11-07 10:43:29

请放心,如果它是GET/HEAD请求,并且状态代码是302,它将自动重定向。在您的情况下,这是一个POST请求,所以它不会自动重定向。您可能需要手动这样做,例如:

代码语言:javascript
复制
Response resp1 =
        given().
            contentType(ContentType.URLENC).
            body("AUTH_TOKEN=&j_username=" + encodedUsername + "&j_password=" + password + "&userName=&AUTH_TOKEN=").
            redirects().follow(false).
        expect().
            statusCode(302).
        when().
            post("/authenticate/j_spring_security_check");

String headerLocationValue = resp1.getHeader("Location");

Response resp2 =
        given().
            cookie(resp1.getDetailedCookie("JSESSIONID")).
        expect().
            statusCode(200).
        when().
            get(headerLocationValue);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74333165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档