It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

400 Bad Request while updating an issue via REST API

Abhishek G Jan 29, 2016

Here is the code that I am attempting to use:

global class SalesforceJiraUpdate {
    public string endpoint='https://xxx.atlassian.net/rest/api/2/issue/';             //JIRA URL
    public String username = 'xxx';        // JIRA username
    public String password = 'xxx';       // JIRA password

    @future(callout=true)
    public static void updateIssue(string key,string summary,string description,string Jiraid){
        key = Jiraid;
        new SalesforceJiraUpdate(key,summary,description,Jiraid);
    }


    public String authHeader(){
        Blob headerValue = Blob.valueOf(username+':'+password);
        return 'Basic ' + EncodingUtil.base64Encode(headerValue);
    }

    public SalesforceJiraUpdate(string key,string summary,string description,string Jiraid){
        HttpRequest req = new HttpRequest();
        Http http = new Http();
        req.setMethod('PUT');

        //Set HTTPRequest header properties

        endpoint+=+key;
        req.setHeader('Accept', 'application/json');
        req.setHeader('Content-Type','application/json');
        Blob headerValue = Blob.valueOf(username+':'+password);
        req.setHeader('Authorization','Basic '+ EncodingUtil.base64Encode(headerValue));
        req.setEndpoint(endpoint);
        system.debug('the endpoint is :'+endpoint);


        String UpdateIssueJson= '{"update": {"summary":[{"set":"'+summary+'"}],"description":[{"set":"'+description+'"}], "issuetype":[{"set" :{"name": "Bug"}}]}}';

        String JSONData = JSON.serializePretty(UpdateIssueJson);
        req.setBody(UpdateIssueJson);
        system.debug('after response....'+UpdateIssueJson);

        try{
            //Execute web service call here
            HTTPResponse res = http.send(req);
            String ResponseJsonString = res.getBody();
            System.debug('ResponseJsonString '+res);

        }
        catch(System.CalloutException e){
            System.debug('Callout error: '+ e);
        }
    }
}

4 answers

1 vote
Abhishek G Feb 04, 2016

Hi Robert, there was issue in framing the Json. I have solved it.

String upJson= '{"fields":{"summary":"'+summary+'","description":"'+description+'"}}';

 

Thanks

Abhishek

0 votes
Robert Massaioli Atlassian Team Jan 29, 2016

Have you tried to get a locally running JIRA and use the Rest API Browser to make your requests?

0 votes
Abhishek G Jan 31, 2016

Robert, am able to create an Issue in JIRA, while updating it response is Bad request and 400 error.

0 votes
Robert Massaioli Atlassian Team Feb 04, 2016

I have tried to duplicate this using Postman on my local machine and have not had an issue:

Screen Shot 2016-02-05 at 9.15.40 am.png

From here I can only think of only two potential issues:

  • does the Issue that you are trying to set the Issue Type of to "Bug" exist inside a project that has the issue type "Bug"?
  • Can you please use a JSON library to build your JSON? If summary or description contain strings that make for invalid JSON then you will get a bad request instead.

Those are the only two issues that I can think of right now that might cause this behaviour. Please tell me if that works / helps. Otherwise, can you please provide screenshots of the Issue that you are trying to update and the contents of the variables that you are setting for your application? Cheers.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Jira Software

Demo Den Ep. 5: Meet Jira Mobile

See how to use Jira on the go! Demo Den Episode 5 is here: meet Jira Mobile with  Jira  Product Manager Rayen Magpantay.  Demo Den is in our monthly series where a Jira PM demonstrates...

413 views 1 10
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you