简体   繁体   English

使用 java 在 Rest Assured 中发送 API 标头

[英]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. API 标头有两个参数 Content-Type=application/json 和 accesstoken = "some_token" 我试图使用放心的 API 自动化但没有成功。 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.获取错误消息为“提供应用程序令牌”和 404 错误代码显示。

Your httpRequest.headers("accesstoken", "kggkgkgkgketdfgxgcccvcdftfty.");你的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您可以将 HashMap 作为正文传递

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"这可能是一个内部 API,它期待“提供应用程序令牌”而不是“访问令牌”
  2. The error code you are getting is 404. So either the service is down or the URL you are using is not correct.您得到的错误代码是 404。所以要么服务已关闭,要么您使用的 URL 不正确。

Hope this helps :)希望这可以帮助 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM