0

I am trying to run POST method to get the access token using REST Assured Code. But it is returning 404 error. My code is as below. POSTMAN Configuration as below same trying to replicate using Rest Assured

Method : POST

Authorization tab: 
             Type : Basic Auth 
             UserName :  ABCD
             Passsowrd : Test@1234

Body Tab : 
           Selecting  "application/x-www-form-urlencode" Radio Button
           Key : grant_type  Value : Client_Credentials
           Key : Scope       value : ABCDAPI


given().auth().basic("Username Here","Password type here")
.header("Authorization", "Basic T1VUUkVBQ0hfQVBJX0NMSUVOVDpIWmRwREwydkR5UE5iQmtvWEdxSkFpK1Qxa08yWSszNndxQXhoYTVXUWhZPQ==n")
.header("Content-Type","application/x-www-form-urlencoded")         
.contentType("application/x-www-form-urlencoded")
.body("[{\"grant_type\":\"client_credentials\"}]")
.body("[{\"scope\":\"ABCDpi\"}]").when()            
.post("https://ABCD.KLM.id.XYZ-Cloud.net/oauth2/access_token?realm=PQR")            
.then().contentType("").statusCode(200);

I am also attaching the screenshot of Postman where it is working enter image description here

ddarellis
  • 3,912
  • 3
  • 25
  • 53

2 Answers2

1

Before trying the below code give yourself some time to look here.

Also application/x-www-form-urlencoded has its POST variables stored as key-value pairs in the body.

given().auth().basic(username, password)
.header("Content-Type","application/x-www-form-urlencoded")
.formParam("grant_type", "client_credentials")
.formParam("scope", "ABCDpi")
.post("https://ABCD.KLM.id.XYZ-Cloud.net/oauth2/access_token?realm=PQR")
.then().contentType("").statusCode(200);
ddarellis
  • 3,912
  • 3
  • 25
  • 53
0

I was seeing your code problem with a problem like yours. And I think discovering one solution for our problems. It might work if you change the follow lines:

.body("[{\"grant_type\":\"client_credentials\"}]")
.body("[{\"scope\":\"ABCDpi\"}]").when()

for this:

.body("grant_type=client_credentials&scope=ABCDpi")

I hope having being helpfull.