Fetching JIRA ticket data in Apex using REST API

Hey folks, I’m stuck trying to get JIRA ticket info in Apex using their REST API. I’ve set up a JQL query but can’t seem to get the right body format to get JSON output. Here’s what I’ve got so far:

public class JiraConnector {
  private static final String BASE_URL = 'https://mycompany.atlassian.net';
  private static final String API_KEY = 'secret_key_here';

  public static HttpResponse fetchTickets(string jqlString) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint(BASE_URL + '/rest/api/3/search');
    req.setMethod('POST');
    req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('apiKey:' + API_KEY)));
    req.setHeader('Accept','application/json');
      
    Map<String, Object> bodyMap = new Map<String, Object>{
        'jql' => jqlString
    };
    req.setBody(JSON.serialize(bodyMap));
    
    return new Http().send(req);
  }
}

When I run it, I get a 415 error saying the body format isn’t right. Any ideas what I’m doing wrong? Thanks!

hey Liam23, looks like ur missing the Content-Type header. try adding this line before sending:

req.setHeader(‘Content-Type’, ‘application/json’);

that should fix the 415 error. lmk if it works!

I encountered a similar issue when working with the JIRA REST API. The 415 error typically indicates an unsupported media type. In addition to setting the Content-Type header as suggested, you might want to adjust your request body structure. JIRA’s API expects a specific format for search requests. Try modifying your bodyMap like this:

Map<String, Object> bodyMap = new Map<String, Object>{
‘jql’ => jqlString,
‘startAt’ => 0,
‘maxResults’ => 50,
‘fields’ => new List{‘summary’, ‘status’, ‘assignee’}
};

This structure allows you to specify additional parameters like the starting point, maximum results, and which fields to return. Adjust the fields list based on the data you need. Remember to handle pagination if you’re dealing with large result sets.

I’ve been working with the JIRA REST API quite a bit lately, and I can share some insights that might help. One thing that’s not immediately obvious is that the ‘/rest/api/3/search’ endpoint actually prefers GET requests for most use cases. Here’s a modified version of your code that’s worked well for me:

public static HttpResponse fetchTickets(String jqlString) {
    HttpRequest req = new HttpRequest();
    req.setEndpoint(BASE_URL + '/rest/api/3/search?jql=' + EncodingUtil.urlEncode(jqlString, 'UTF-8'));
    req.setMethod('GET');
    req.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('apiKey:' + API_KEY)));
    req.setHeader('Accept', 'application/json');
    
    return new Http().send(req);
}

This approach eliminates the need for a request body, simplifying the process and avoiding potential formatting issues. Just make sure to URL encode your JQL string to handle special characters. If you need to specify fields or pagination, you can add those as query parameters too. Hope this helps!