简体   繁体   中英

How to get the access token of REST API endpoint using Rest Assured with POST method, Currently I am getting 404 error?

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

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);

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.

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