Error making a POST request with Springboot

Asked

Viewed 84 times

0

I have the code below:

String url = new StringBuilder().append("https://...").toString();
Map<String, Map<String, String>> body = buildBotBody(email,message);
restTemplate.postForEntity(url, body, Void.class);

This request does not need authentication, IE no login and password. Making this request in Postman works perfectly. But when I run the above code, I get the error below:

401 Unauthorized

I did other tests and managed to simulate the error in Postman. He informed the message below:

"message": "The request has both SAS authentication scheme and 'Basic' authorization scheme. Only one scheme should be used."

I realized that if I change the authentication form to "No Auth" in Postman, the request works.

I think the code would have to set this "No Auth" somehow.


More details of the code:

private HttpEntity<?> builderHeadersToBuildBotBody(String email, StringBuilder message) {
    
    Map<String, String> map = new HashMap<String, String>();
    map.put("emailadress", email);
    map.put("emailSubject", "Pendência para lançamento de horas do Jira");
    map.put("emailBody", message.toString());
    
    return builderHeaders(map);
}

private HttpEntity<?> builderHeaders(Map<String, String> map) {
    
    HttpHeaders requestHeaders = new HttpHeaders();
    //requestHeaders.setAccept(Collections.singletonList(new MediaType("application", "json")));
    requestHeaders.setContentType(new MediaType("application", "json"));
    //requestHeaders.set(HttpHeaders.USER_AGENT, "");
    return new HttpEntity<>(requestHeaders);
}

private void sendBotMessage(StringBuilder message, String nome, String email) throws Exception{
    
    try {
        String url = new StringBuilder().append("https://prod-12.westeurope.logic.azure.com:443/workflows/fbf4c29cbcad4679b1a1159fff7b07f9/triggers/manual/paths/invoke?api-version=2016-06-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig=zxm46aQnBj3ZTKPOddnnwUgtQZoQcQfixNtXVxAJjPg").toString();
        HttpEntity<?> requestEntity = builderHeadersToBuildBotBody(email,message);
        restTemplate.exchange(url, HttpMethod.POST, requestEntity, Void.class);
        logger.info("Bot enviado com sucesso! " + nome);
    } catch (Exception e) {
        logger.error("Erro ao enviar Bot.", e);
        throw e;
    }
    
}

But that way it didn’t work either.

  • Your restTemplate has no Interceptor configuration to include an authorization header when making the call?

  • I will show above how I did using header.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.