1

I'm trying to create an issue in Redmine using Google Apps Script, the code is following:

function create_issue() {
  var payload = {
    'project_id': 'helpdesk',
    'subject': 'This is a test ticket',
    'description': 'This is just a genius test ticket',
    'category_id': 1
  };
  var headers = {
    'X-Redmine-API-Key': '<myapikey>',
    'Content-Type': 'application/json'
  };
  var url = 'http://myredmine.com/issues.json';
  var options = {
    'method': 'POST',
    'headers': headers,
    'payload': payload,
    //uteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

Every time I ran that script, it threw the following exception:

Execution failed: Request failed for http://myredmine.com/issues.json returned code 422. Truncated server response: {"errors":["Subject can't be blank"]} (use muteHttpExceptions option to examine full response) (line 25, file "Code") [0.645 seconds total runtime]

But as you see, the "subject" parameter was passed in the payload already. What am I missing?

Thanks,

Trinh

Trinh Nguyen
  • 1,445
  • 1
  • 14
  • 22

1 Answers1

3

I found the problem, I need to indicate the issue in the payload:

function create_issue() {
  var issue = {
    "description": "Test ticket", 
    "subject": "Genius ticket"
  }
  var payload = {
    "issue": issue, 
    "key": "<myapikey>", 
    "project_id": "helpdesk",
  };
  payload = JSON.stringify(payload);
  var headers = {
    'X-Redmine-API-Key': '<myapikey>',
    'Content-Type': 'application/json'
  };
  var url = 'http://myredmine.com/issues.json';
  var options = {
    'method': 'POST',
    'headers': headers,
    'payload': payload,
    'contentType': 'application/json',
    //'muteHttpExceptions': true
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response);
}

And it works!

Trinh Nguyen
  • 1,445
  • 1
  • 14
  • 22