gpt4 book ai didi

java - 如何在 RestAssured 中将基本身份验证作为请求 header 的一部分传递?

转载 作者:行者123 更新时间:2023-12-02 10:27:44 25 4
gpt4 key购买 nike

我是 API 测试和 RestAssured 的新手。我一直在尝试访问API(受基本身份验证保护)http://restapi.demoqa.com/authentication/CheckForAuthentication通过 RestAssured 但不知道该怎么做。

这是我到目前为止编写的代码:

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

RestAssured.baseURI = "http://restapi.demoqa.com/authentication/CheckForAuthentication";
RequestSpecification request = RestAssured.given();

**I guess authentication code goes here but not sure**
Response response = request.get();

最佳答案

在 RestAssured 中,您可以这样做:

RequestSpecification request = RestAssured.given().auth().basic("username", "password");
Response response = request.get()

还可以进行抢先身份验证:

RequestSpecification request = RestAssured.given().auth().preemptive().basic("username", "password");
Response response = request.get()

官方 wiki 页面上有有关 RestAssured 身份验证的更多信息:https://github.com/rest-assured/rest-assured/wiki/Usage#authentication

关于java - 如何在 RestAssured 中将基本身份验证作为请求 header 的一部分传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53800878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com