0

I'm trying to edit an issue in JIRA through GAS. Looking at some other people code (for example - Using Google Apps Script to Post JSON Data) I came up with this code:

function myFunctionpostTest() {
  var username = "username";
  var password = "password";
  var encCred = Utilities.base64Encode(username+":"+password);

  var url = "https://<base_url>/rest/api/2/issue/";
  var data = {"project":{ "key": "STUDIO-4499"},"summary": "create 
  issue.", "issuetype": {"name": "Bug"}}  ;
  var payload = JSON.stringify(data);

  var headers = { "Accept":"application/json", 
              "Content-Type":"application/json", 
              "Authorization":"Basic " + encCred,
         };

  var options = { "method":"POST",
              "contentType" : "application/json",
              "headers": headers,
              "payload" : payload
           };
  var response = UrlFetchApp.fetch(url, headers);
  Logger.log(response); 
 }

The issue is that i'm keep getting an error:

Request failed for.... returned code 405

What am i missing? why this code is not working?
Please don't answer with cURL example since it is not relevant for my issue

Rea Krakover
  • 113
  • 2
  • 13

2 Answers2

0

It seems that options is not used in the script. So how about a modification as follows? But I don't know whether the option is correct for the request. I'm sorry for this.

From :

var response = UrlFetchApp.fetch(url, headers);

To :

var response = UrlFetchApp.fetch(url, options);

If this was not useful for you, I'm sorry.

Edit :

How about the following modified script? Reference is here.

function myFunctionpostTest() {
    var username = "username";
    var password = "password";
    var encCred = Utilities.base64Encode(username+":"+password);
    var url = "https://<base_url>/rest/api/2/issue/";
    var data = {
      "fields": {
        "project": {
          "key": "STUDIO-4499"
        },
        "summary": "create \r\n  issue.",
        "issuetype": {
          "name": "Bug"
        }
      }
    };
    var payload = JSON.stringify(data);
    var headers = {"Authorization":"Basic " + encCred};
    var options = {
      "method":"POST",
      "contentType": "application/json",
      "headers": headers,
      "payload": payload
    };
    var response = UrlFetchApp.fetch(url, options);
    Logger.log(response);
}
Tanaike
  • 181,128
  • 11
  • 97
  • 165
0

using this reference -
https://docs.atlassian.com/jira/REST/7.4.0/?_ga=2.214927127.1280782706.1510046526-258513799.1499779287#api/2/issue-editIssue

i've changed the data variable to

{"update":{"summary":[{"set":"Bug in business logic"}]}}; 

the url to

var url = "https://<base URL>/rest/api/2/issue/41335";

and changed the method in the options var to

"method":"PUT",

And now it works!

Rea Krakover
  • 113
  • 2
  • 13