Spring Boot Jira API authentication returns 401 error despite working in Postman

I’m able to authenticate with the Jira API through Postman without any issues. However, when I try to implement this authentication in my Spring Boot application, I repeatedly encounter a 401 unauthorized error. Here’s the relevant code I’ve been working with:

public class ApiClient {

    private String baseUrl = "mycompany.atlassian.net";
    private RestTemplate restTemplate;
    private HttpHeaders httpHeaders;
    private HttpStatusCode responseStatus;

    public ApiClient() {
        this.restTemplate = new RestTemplate();
        this.httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        httpHeaders.add("Accept", "*/*");
        httpHeaders.add("Connection", "keep-alive");

        String authString = "Basic " + new String(Base64.getEncoder().encode("myuser:mytoken".getBytes()));
        System.out.println(authString);
        httpHeaders.add("Authorization", authString);
    }

    public String makeGetRequest(String endpoint) {
        HttpEntity<String> entity = new HttpEntity<String>("", httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(baseUrl + endpoint, HttpMethod.GET, entity, String.class);
        this.setResponseStatus(response.getStatusCode());
        return response.getBody();
    }

    public HttpStatusCode getResponseStatus() {
        return responseStatus;
    }

    public void setResponseStatus(HttpStatusCode status) {
        this.responseStatus = status;
    }

    public String fetchIssueStatus(String issueKey) throws JSONException {
        ApiClient client = new ApiClient();
        String result = client.makeGetRequest("/rest/api/2/issue/" + issueKey + "?fields=status");
        
        JSONObject json = new JSONObject(result);
        JSONObject fields = (JSONObject) json.get("fields");
        JSONObject statusObj = (JSONObject) fields.get("status");
        String statusName = statusObj.getString("name");
        
        return statusName;
    }
}

I even tried hardcoding the authorization header directly, but still faced the same issue. The credentials I use work perfectly when tested in Postman, so I’m puzzled about what’s going wrong in my Spring Boot implementation.

You’re probably missing the protocol in your baseUrl. Try https://mycompany.atlassian.net instead of just mycompany.atlassian.net. RestTemplate needs the full URL to work properly. I had the same problem and it turned out the missing protocol was causing auth failures. Also check that your API token hasn’t expired - they can expire without warning. Postman might still work because it’s using a cached session, but your Spring Boot app makes fresh requests every time. Turn on debug logging to see exactly what URL it’s calling - that’s how I caught my protocol issue.

Hit this same issue last week. Your base64 encoding’s off - you’re encoding “myuser:mytoken” but Jira wants “email:apitoken” format. That extra new String() wrapper around the Base64 output is probably breaking it too. Skip the manual encoding and use HTTPBasicAuth from Spring Security instead - much cleaner and won’t bite you later.

I’ve hit this exact issue with Jira API and Spring Boot. Your Basic auth header construction is probably the culprit. Skip the manual encoding and use Spring’s BasicAuthenticationInterceptor instead. Also, make sure you’re using an API token from your Atlassian account settings - not your regular password. Check if your Postman request hits the same endpoint too. Postman auto-follows redirects while RestTemplate doesn’t. Add some interceptors to log your actual HTTP requests with all headers. Compare what Spring Boot sends vs what Postman sends. Could also be user agent strings or other headers that Postman adds automatically but your code misses.

This topic was automatically closed 4 days after the last reply. New replies are no longer allowed.