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!
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:
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:
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!