POST请求在Postman中得到了307响应,无法在RestAssured中复现。

huangapple go评论73阅读模式
英文:

POST request in postman got 307, unable to reproduce in restassured

问题

我有一个POST请求,在Postman中首先尝试了一下,我想在重定向之前捕获状态码。在Postman中,我得到了307状态码(我设置了不跟随重定向的设置)。

[Postman][1]


  [1]: https://i.stack.imgur.com/iiB3I.png

但是当我尝试使用restassured时,它仍然被重定向,所以我得到了200状态码,而不是307。

用相同的方式尝试了带有302状态码的GET请求,那个是可以工作的。

    public void postDataBeforeLogin() {
		
		// 在登录之前提交数据
		Response response = RestAssured.given().redirects().follow(false).post("/data");
		
		assertEquals(response.getStatusCode(), 307); 

	}

我读了一篇关于restassured不重定向POST请求的文章/帖子,但那是3年前的事了,所以我不确定现在是否仍然是这样的情况。

有人能帮忙/澄清吗?

非常感谢您的帮助!
英文:

I have a POST request, which I tried first in Postman, I wanted to capture the status code before it's redirected. In Postman, I got 307 (I set the settings so it doesn't follow redirects).

Postman

But when I tried using restassured, it still got redirected, so I got 200 status code instead of 307.

Tried the same way with GET request with 302 status code, and that one works.

public void postDataBeforeLogin() {
	
	//post data before login
	Response response = RestAssured.given().redirects().follow(false).post("/data");
	
	assertEquals(response.getStatusCode(), 307); 

}

I read an article/post about restassured not redirecting POST requests, but it was from 3 years ago, so I'm not sure if that is still the case.

Can anyone help/clarify?

Help will be greatly appreciated, thank you!

答案1

得分: 1

RestAssuredConfig会满足您的需求,就像这样:

given().config(RestAssured.config().redirect(redirectConfig().followRedirects(false))).

在您的情况下:

import static io.restassured.config.RedirectConfig.redirectConfig;

Response response = RestAssured.given()
                    .config(RestAssured.config().redirect(redirectConfig().followRedirects(false)))
                    .post("/data");

了解更多:REST-assured wiki

推荐静态导入方法,参见这里

英文:

RestAssuredConfig will meet your requirements, like this:

given().config(RestAssured.config().redirect(redirectConfig().followRedirects(false))).

in your case:

import static io.restassured.config.RedirectConfig.redirectConfig;

Response response = RestAssured.given()
                    .config(RestAssured.config().redirect(redirectConfig().followRedirects(false)))
                    .post("/data");

See more: REST-assured wiki

It's recommended to statically import methods, see this

huangapple
  • 本文由 发表于 2020年9月7日 20:26:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63777604.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定