如何从 RestAssured 响应返回位置 header

How to have location header returned from RestAssured response

我正在使用 RestAssured,我需要获取我的 api 返回的位置 header 下设置的值,以便我可以在下一个请求中将其用作不记名令牌.

我尝试了一些类似 cookieHeader 的方法,但我认为不是这样。

    Response response = getRequestSpecification()
            .queryParam("myparam", "id_token")
            .queryParam("redirect_uri", "https%3A%2F%2Fmyurl.localhost%3A3000%2Fview")
            .get(OKTA_OAUTH2_AUTHORIZE);


public static RequestSpecification getRequestSpecification() {

        /** Enables printing request as curl under the terminal as per https://github.com/dzieciou/curl-logger */
        Options options = Options.builder()
                .printMultiliner()
                .updateCurl(curl -> curl
                        .removeHeader("Host")
                        .removeHeader("User-Agent")
                        .setCookieHeader("location")
                        .removeHeader("Connection"))

                .build();

        RestAssuredConfig config = CurlRestAssuredConfigFactory.createConfig(options).objectMapperConfig(new ObjectMapperConfig(ObjectMapperType.GSON));


        RequestSpecification rq = given()
                .config(config)
                .contentType(ContentType.JSON)
                .cookie("location")
                .when()
                .log()
                .everything();

        return rq;
    }

我希望我的 curl 像这样添加 --location:

curl --location --request GET 'https:myapi.com' \

完成后,我还需要将 -i 添加到其中。

Ps:我正在使用 this library 打印卷曲。

非常感谢你。

更新:我认为它可能与禁用重定向有关,因为当我在 Postman 上启用此功能时,我得到了400 并且没有 header 返回,但是当它们关闭时,我得到一个 302,它也给了我位置。然后我尝试将其添加到我的请求中。

.redirects().follow(false)

所以我有

Response response = getRequestSpecification()
        .queryParam("myparam", "id_token")
        .redirects().follow(false)
        .get(OKTA_OAUTH2_AUTHORIZE);

但我仍然收到 400 错误。

现在可以了。有两个问题:

  • 正在尝试重定向到 redirect_uri,所以我不得不禁用它。

  • url 的编码方式与 Postman 使用的方式不同,所以我也禁用了它并添加了它,它已经手动编码为我需要的参数。

    Response response = getRequestSpecification()
                  .queryParam("myparam", "id_token")
                  .queryParam("redirect_uri", "https%3A%2F%2Fmyurl.localhost%3A3000%2Fview")
                  .redirects().follow(false)
                  .urlEncodingEnabled(false)
                  .get(OKTA_OAUTH2_AUTHORIZE);