How to fetch JIRA tickets using REST API in Salesforce Apex

I’m working on connecting to JIRA from Salesforce and need to execute JQL queries through their REST API. My current implementation isn’t working properly and I keep getting format errors.

Here’s what I have so far:

public class AtlassianClient {
    private static final String BASE_ENDPOINT = 'https://mycompany.atlassian.net';
    private static final String AUTH_TOKEN = 'my_token_here';
    
    public static HttpResponse fetchTickets(String queryString) {
        HttpRequest req = new HttpRequest();
        req.setEndpoint(BASE_ENDPOINT + '/rest/api/3/search');
        req.setMethod('POST');
        req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('apiToken:' + AUTH_TOKEN)));
        req.setHeader('Accept', 'application/json');
        
        Map<String, Object> bodyData = new Map<String, Object>{
            'jql' => queryString
        };
        String jsonBody = JSON.serialize(bodyData);
        req.setBody(jsonBody);
        
        System.debug(jsonBody);
        return new Http().send(req);
    }
}

I’m testing it like this:

HttpResponse result = AtlassianClient.fetchTickets('project = "TEST"');
if (result.getStatusCode() == 415) {
    String body = result.getBody();
    System.debug('Error Response: ' + body);
    
    List<String> headers = result.getHeaderKeys();
    for (String headerName : headers) {
        System.debug('Header: ' + headerName);
    }
} else {
    String body = result.getBody();
    System.debug('Success Response: ' + body);
}

The response keeps telling me the body format is unsupported. What am I doing wrong with the request structure?

Your Basic auth format is wrong. JIRA API tokens need email:token, not apiToken:token. Change your authorization header to req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('[email protected]:' + AUTH_TOKEN)));. And double-check you’re using an actual API token from JIRA account settings, not your password. Had the exact same problem when I first set up JIRA with Salesforce - this fixed it. That 415 error is misleading when it’s really an auth issue.

you probly need to add the content-type header. jira’s api is super picky. try adding req.setHeader('Content-Type', 'application/json'); right before sending your request. that usually fixes those 415 errors even if your json seems alright.

The problem’s with your endpoint and request method. You’re using POST to /rest/api/3/search, but JIRA’s search endpoint works better with GET requests where you pass the JQL as a URL parameter.

Try switching to GET and append the JQL as a query parameter: req.setEndpoint(BASE_ENDPOINT + '/rest/api/3/search?jql=' + EncodingUtil.urlEncode(queryString, 'UTF-8')); Then ditch the body completely.

I’ve hit this exact issue before - the API expects GET for search operations even though POST sometimes works. That 415 error usually pops up when you send a body with GET or when the endpoint wants parameters instead of JSON payload.

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