简体   繁体   中英

How to send a GET request with Authentication header in REST Assured?

I am trying to send a GET request to a URL using header "Auth-Service-Id: my_app" , "Auth-Identifier: blaBlaBlabla" , "Auth-Secret: myauth-secret" using REST Assured, JUnit.

public class RestAPITest {

@Test
public void getRequestTime() throws URISyntaxException {

    // Specify the base URL to the RESTful web service
    Response response = RestAssured.get("https://myurl.com");        
    logger.log(Level.INFO, response.time() + " milliseconds");

    int statusCode = response.getStatusCode();
    logger.log(Level.INFO, "Expected response status code is 200. Recieved response status code is " + statusCode);
    Assert.assertEquals(statusCode, 200);
    logger.log(Level.INFO, "Response Body is " + response.body().asString());
}

You can pass header using headers() method of RestAssured like:

Map<String, String> headers = new HashMap<>();
    headers.put("Auth-Service-Id", "my-app");
    headers.put("Auth-Identifier", "blaBlaBlabla");

Response response = RestAssured.given(this.requestSpecification)
            .headers(headers)
            .when()
            .get("https://myurl.com");

For further reading follow this link.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM