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?