gpt4 book ai didi

java - 使用 java 在 Rest Assured 中发送 API header

转载 作者:行者123 更新时间:2023-11-30 05:20:31 27 4
gpt4 key购买 nike

API header 有两个参数 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);

获取错误消息“提供应用程序 token ”并显示 404 错误代码。

最佳答案

您的 httpRequest.headers("accesstoken", "kggkgkgkgketdfgxgccccdftfty."); 是错误的。应该是:

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

关于java - 使用 java 在 Rest Assured 中发送 API header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59660814/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com