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 = "";
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")
1

I hope below code can help you to resolve authentication issue given().relaxedHTTPSValidation().auth().preemptive().basic("username", "token(in base 64 encryption")

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest