简体   繁体   中英

Send the API Headers in Rest Assured using java

API Headers have two parameter Content-Type=application/json and also accesstoken = "some_token" I trying to automate the API using Rest assured but not successful. Below is the code

RestAssured.baseURI = prop.getProperty("serviceurl1");

//2. define the http request:
RequestSpecification httpRequest = RestAssured.given()  
                .filter(new ResponseLoggingFilter())
                .filter(new RequestLoggingFilter());

JSONObject requestParams = new JSONObject();
requestParams.put("longitude", eLongitude);
requestParams.put("latitude", eLaititude);
requestParams.put("country", eCity);

httpRequest.headers("Content-Type", "application/json");
httpRequest.headers("accesstoken", "some_token.");
httpRequest.body(requestParams.toJSONString());
int statusCode = response.getStatusCode();
System.out.println("the status code is: "+ statusCode);

Assert.assertEquals(statusCode, TestUtil.RESPONSE_CODE_200);

System.out.println("the status line is: "+ response.getStatusLine());

//6. get the headers:
Headers headers = response.getHeaders();
System.out.println(headers);

String contentType = response.getHeader("Content-Type");
System.out.println("the value of content-type header is: "+ contentType);

String contentLength = response.getHeader("Content-Length");
System.out.println("the value of Content-Length header is: "+ contentLength);

Getting error message as "Provide Application Token" and 404 error code display.

Your httpRequest.headers("accesstoken", "kggkgkgkgketdfgxgcccvcdftfty."); is wrong. It should be:

 httpRequest.headers("Authorization", "Bearer "+token);

can you try this once

Response resp = given().when().contentType(ContentType.JSON).header("accesstoken", "token").body(body).put("url");

You can pass the HashMap as body

These are the issues I can think of

  1. This might be an internal API and it is expecting "Provide Application Token" and not the "accesstoken"
  2. The error code you are getting is 404. So either the service is down or the URL you are using is not correct.

Hope this helps :)

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