我正在尝试使用Google Apps脚本在Redmine中创建一个问题,代码如下:
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);
}
每次我运行该脚本时,都会抛出以下异常:
执行失败:http://myredmine.com/issues.json请求失败 返回代码422.截断的服务器响应:{“errors”:[“Subject 不能为空“”}(使用muteHttpExceptions选项来检查完整 响应)(第25行,文件“代码”)[总运行时间为0.645秒]
但是如您所见,“subject”参数已经在有效负载中传递。我错过了什么?
谢谢,
的Trinh
答案 0 :(得分:3)
我发现了问题,我需要在有效载荷中指出问题:
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);
}
它有效!