我在这个问题上被困住了,请帮我.
所以,邮政要求工作良好的邮递员,但在放心,我得到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();
RestAssured.config = config().redirect(redirectConfig().followRedirects(true).and().maxRedirects(0));发布于 2022-11-07 10:43:29
请放心,如果它是GET/HEAD请求,并且状态代码是302,它将自动重定向。在您的情况下,这是一个POST请求,所以它不会自动重定向。您可能需要手动这样做,例如:
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);https://stackoverflow.com/questions/74333165
复制相似问题