How to pass a Bearer token in Rest assured?
- Quality Engineering
How to pass a Bearer token in Rest assured?
What is the Authorization Header?
The Authorization request header includes credentials to authenticate the client on the server. HTTP provides a built-in framework for user authentication and controlling access to protected resources
Bearer token
Bearer tokens enable requests to authenticate using an access key, such as a JSON Web Token (JWT). The token is a text string, included in the request header. In the request Authorization tab, select Bearer Token from the Type drop-down list. In the Token field, enter your API key value. For added security, store it in a variable and reference the variable by name.Postman will append the token value to the text Bearer in the required format to the request Authorization header.
Bearer Token Authentication Example
Authorization: Bearer {token}
Sending Request with Bearer Token Authorization Header
To send a request with a Bearer Token authorization header, you need to make an HTTP GET or POST request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header. Bearer Authentication (also called token authentication) is an HTTP authentication scheme created as part of OAuth 2.0 but is now used on its own. For security reasons, bearer tokens are only sent over HTTPS (SSL).
Example
Response response = given().header((“Authorization”,”Bearer “+token)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
@Test(priority = 1) public void ValidateToken() { Response response = given().header("Content-Type", "application/json") .body("{\n" + " \"mobile\": \"2022014667\",\n" + " \"otp\": \"2432\"\n" + "}") .when().post("https://api.phedmanipur.com/api/v1/user/validateOtp"); Assert.assertEquals(response.statusCode(), 200); Assert.assertEquals(response.body().jsonPath().getString("message"), "User Verified Successfully"); response.prettyPrint(); token = response.body().jsonPath().getString("data.accessToken"); } @Test(priority = 2) public void getUserProfileData() { Response response = given().header("Content-Type", "application/json") .header("Authorization", "Bearer " + token) .body("{\n" + " \"siteId\": \"1522000580\"\n" + "}") .when().post("https://api.phedmanipur.com/api/v1/user/getUserProfileData"); Assert.assertEquals(response.statusCode(), 200); Assert.assertEquals(response.body().jsonPath().getString("message"), "Data retrieved successfully"); response.prettyPrint(); } |
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s