简体   繁体   English

如何在Java中为Get请求将令牌设置为HTTPHeader?

[英]How do you set a token as an HTTPHeader for a get request in java?

I'm trying to write a test to receive a JSON response from an API and I need to set a security token in the header for the API call. 我正在尝试编写测试以从API接收JSON响应,并且需要在API调用的标头中设置安全令牌。 I've already verified that I am receiving a valid token from the get/token API. 我已经验证我从get / token API收到了有效的令牌。 When I try to execute the HttpGet I am receiving a 401 status code. 当我尝试执行HttpGet时,我收到401状态代码。 Update: Does anyone have a complete list of authorization token types? 更新:有人有授权令牌类型的完整列表吗?

public void listAllDoctors() throws IOException {

    String listAllDoctors = "/api/doctors/search";

    HttpGet getDEV = new HttpGet(DEVBASE_ENDPOINT + listAllDoctors);
    getDEV.setHeader(HttpHeaders.AUTHORIZATION, "token " + TOKEN);
    getDEV.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");

    response = client.execute(getDEV);

    int actualStatus = response.getStatusLine().getStatusCode();
    assertEquals(actualStatus, 200);
}

I figured out that the API uses a custom header token authentication. 我发现该API使用了自定义标头令牌身份验证。 So the line of code goes like this: 因此,代码行如下所示:

getDev.setHeader("token", "Token value goes here");

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

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