TMO
TMO

Reputation: 161

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. I've already verified that I am receiving a valid token from the get/token API. When I try to execute the HttpGet I am receiving a 401 status code. 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);
}

Upvotes: 0

Views: 5277

Answers (1)

TMO
TMO

Reputation: 161

I figured out that the API uses a custom header token authentication. So the line of code goes like this:

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

Upvotes: 1

Related Questions