Sharma
Sharma

Reputation: 91

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized

I have web service URL:

http://myservice.local/aprovalanduser/?format=json&Name=India

When I am calling this URL using

resttemplate httpsrestTemplate.getForObject(uri, userdetails[].class)

I am getting error:

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized

in the web service method:

method: "GET", 
data: xmlData, 
contentType: "application/xml", 
dataType: "xml", 
async: true, 
crossDomain: false,

I am setting the header only for XML like below:

headers.setContentType(MediaType.APPLICATION_XML);

Upvotes: 8

Views: 78543

Answers (3)

String plainCreds_usuario = Constants.CREDENCIAL_REST_API_ODATA_USUARIO;
        String plainCreds_password = Constants.CREDENCIAL_REST_API_ODATA_PASSWORD;
        
        String plainCreds = plainCreds_usuario+":"+plainCreds_password;
        
        byte[] plainCredsBytes = plainCreds.getBytes();
        byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
        String base64Creds = new String(base64CredsBytes);

        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Basic " + base64Creds);
        HttpEntity<String> request = new HttpEntity<String>(headers);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<Object> response = restTemplate.exchange(Constants.getRutaApiOdataHttp() + Constants.WS_REST_API_ODATA_GET_EMPRESAS, HttpMethod.GET, request, Object.class);
        response.getBody();
        
        if(response != null && response.getBody() != null){             
            System.out.println("YES");
            System.out.println(response.getBody().toString());
        }
        else
        {
            System.out.println("NONES");
        }

Upvotes: 1

Venom
Venom

Reputation: 31

Here is the code which does basic authentication as suggested by @ekem chitsiga

String plainCreds = "username:password";
byte[] plainCredsBytes = plainCreds.getBytes();
byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
String base64Creds = new String(base64CredsBytes);

HttpHeaders headers = new HttpHeaders();
headers.add("Authorization", "Basic " + base64Creds);
HttpEntity<String> request = new HttpEntity<String>(headers);

RestTemplate restTemplate = new RestTemplate();
String url = "http://myservice.local/aprovalanduser/?format=json&Name=India";
ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, request, Object.class);
response.getBody();

Upvotes: 3

ekem chitsiga
ekem chitsiga

Reputation: 5753

Http status code 401 means that you need to supply credentials to access the service. The way you present the credentials dependends on the authentication mechanism used by the service

For example if it uses Basic Authentication then you need to add a Authorization request header with Basic prefix and a base64 encoded combination of username and password separated by :

Upvotes: 2

Related Questions