Basic authentication for REST API using spring restTemplate

前端 未结 8 1252
眼角桃花
眼角桃花 2020-11-29 21:02

I am completely new in RestTemplate and basically in the REST APIs also. I want to retrieve some data in my application via Jira REST API, but getting back 401 Unauthorised.

8条回答
  •  一整个雨季
    2020-11-29 21:11

    Use setBasicAuth to define credentials

    HttpHeaders headers = new HttpHeaders();
    headers.setBasicAuth("myUsername", myPassword);
    

    Then create the request like you prefer.

    Example:

    HttpEntity request = new HttpEntity(headers);
    ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, 
    request, String.class);
    String body = response.getBody();
    

提交回复
热议问题