How to Use Basic Authentication with Rest Assured in This Situation
I Am Trying to Use Authentication with Restassured. Here Is My Code That Does Not Work: Public Static Void Authenticate(){ Restassured. baseURI = ""...
I am trying to use authentication with RestAssured. Here is my code that does not work:
public static void authenticate(){
RestAssured.baseURI = "";
RequestSpecification request = RestAssured.given().auth().basic("[email protected]", "password123");
Response response = request.get();
System.out.println(response.asString());}
A big reason for which this does not work is that I am missing certain things because when i look at the actual request in postman, there is more info in the tabs there as follows:
Authorization: Basic Auth:
Username:"client"
Password:"pass"
Headers:
Authorization: Basic Y2xpZW50OnBhc3N3b3Jk
Content-Type: application/x-www-form-urlencoded
Body:
username:"[email protected]"
password:"password123"
grant_type:"password"
My question is what are the missing pieces in my code and how do integrate them so that the authorization works?
Thank you
2 Answers
REST Assured doesn't send the credentials when using basic auth unless it's challenged by the server. If the server doesn't challenge, it won't send it. I this is the case you can use preemptive basic auth:
RestAssured.given().auth().preemptive().basic("[email protected]", "password123")
I hope below code can help you to resolve authentication issue given().relaxedHTTPSValidation().auth().preemptive().basic("username", "token(in base 64 encryption")