繁体   English   中英

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

[英]Send the API Headers in Rest Assured using java

API 标头有两个参数 Content-Type=application/json 和 accesstoken = "some_token" 我试图使用放心的 API 自动化但没有成功。 下面是代码

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

获取错误消息为“提供应用程序令牌”和 404 错误代码显示。

你的httpRequest.headers("accesstoken", "kggkgkgkgketdfgxgcccvcdftfty."); 是错的。 它应该是:

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

你能试试这个吗

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

您可以将 HashMap 作为正文传递

这些是我能想到的问题

  1. 这可能是一个内部 API,它期待“提供应用程序令牌”而不是“访问令牌”
  2. 您得到的错误代码是 404。所以要么服务已关闭,要么您使用的 URL 不正确。

希望这可以帮助 :)

暂无
暂无

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

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