0

So I am trying to retrieve output of JQL query using in Apex using API provided by Jira. However I am not able to format my body such that I receive the expected output in JSON file. here is my code

public class JiraRestClient {

  private static final String JIRA_BASE_URL = 'https://sregmi48.atlassian.net';
  private static final String JIRA_API_TOKEN = 'my_api_code';

  public static HttpResponse getAllIssues(string jqlQuery) {
    HttpRequest request = new HttpRequest();
    request.setEndpoint(JIRA_BASE_URL + '/rest/api/3/search');
    request.setMethod('POST');  // Use GET for searching issues by project key
    request.setHeader('Authorization', 'Basic ' + EncodingUtil.base64Encode(Blob.valueOf('apiToken:' + JIRA_API_TOKEN)));
    request.setHeader('Accept','application/json');
      
    Map<String, Object> requestBodyMap = new Map<String, Object>{
        'jql' => jqlQuery
    };
    String requestBodyJSON = JSON.serialize(requestBodyMap);      
//  String requestBody = '{"jql":{"project":{"key":"' + projectKey + '"}}}';
    request.setBody(requestBodyJSON);
    
      
    System.debug(requestBodyJSON);  

    return new Http().send(request);
  }
}

Executing this code in the Anonymous window with

HttpResponse response = JiraRestClient.getAllIssues('project = "JIR"');
if (response.getStatusCode() == 415) {
String responseBody = response.getBody();
System.debug('Response Body: ' + responseBody);

List<String> responseHeaders = response.getHeaderKeys();
for (String key : responseHeaders) {

        System.debug('Response Header ' + key );
    }

} else {
String responseBody = response.getBody();
System.debug('Response Body: ' + responseBody);
}

However, I don't get any value in my JSON file. And troubleshooting the issue shows that my response body format is not supported. enter image description here

How do I solve this problem?

5
  • step 1: get this working in your favorite command-line or web-based API client. Step 2: port to Apex. Commented May 8, 2024 at 16:41
  • could you give me some reference for Step 2? Commented May 9, 2024 at 6:18
  • 2
    Step 2: copy all headers, body, et cetera from your successful call to Apex. Commented May 9, 2024 at 6:25
  • Hey, I set everything like you said. However, I am encountering the status code 405 'Client Must be Authorised to Access the Resource' error. I am the one who created those Projects in JIRA and yet I am not able to access the the project from Apex. Commented May 10, 2024 at 4:35
  • 1
    Please edit your question and show a successful call from another client, let's compare that to Apex. Commented May 10, 2024 at 15:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.