Setting API key in Rest Assured

1,676 views
Skip to first unread message

MK

unread,
May 17, 2016, 2:35:59 PM5/17/16
to REST assured
Have a requirement to use API key in place of password for Rest API authentication. Is there a way to set API key in Rest assured?

 Thanks!

Johan Haleby

unread,
May 17, 2016, 3:35:39 PM5/17/16
to rest-a...@googlegroups.com
I would certainly think so. But in order to answer that I must know how you are supposed to provide this api key to the server?


tisdag 17 maj 2016 skrev MK <meena...@gmail.com>:
Have a requirement to use API key in place of password for Rest API authentication. Is there a way to set API key in Rest assured?

 Thanks!

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Sent from my phone

anwesana kanungo

unread,
May 30, 2016, 7:44:58 AM5/30/16
to REST assured
Hi ,

You can do it like this .
public class AddApplicationTypeApiTest {


/**
* Created by anwesana.k on 4/28/2016.
*/



String tokendetails;
String url="https://10.2.0.183:9191/v1.0/ui/applicationType";
@BeforeSuite()
public void beforeexecution(){
String loginurl = "https://10.2.0.183:9191/v1.0/ui/user/authenticate";
Map<String, Object> jsonAsMap = new HashMap<String, Object>();
jsonAsMap.put("userName", "admin");
jsonAsMap.put("password", "KICekGmsle4brMVtBhVgZA==");
jsonAsMap.put("initializationVector","73b6569f3e4b3f95");

Response response2 = given().relaxedHTTPSValidation().contentType("application/json").
body(jsonAsMap).
when().
post(loginurl).then().statusCode(200).extract().response();
tokendetails = response2.getBody().jsonPath().getString("authToken");
System.out.println("Result-------------" +tokendetails);
}

@Test
public void responcecodevalidation(){


Response response1 = given().relaxedHTTPSValidation().header("Authorization",tokendetails).contentType("application/json").
body("{\n" +
"\"name\":\"TestApi\",\n" +
"\"description\":\"TestApi Description\",\n" +
"\"status\":1,\n" +
"\"componentVersionIds\" : [1,2]\n" +
"}").
when().
post(url);
int responsecode =response1.getStatusCode();
System.out.println(responsecode);
Assert.assertEquals(200, responsecode);

}
Reply all
Reply to author
Forward
0 new messages