0

After a little help in regards to posting a new message to the Basecamp API using Google Script.

I have created a function, pasted below but when running this through the debugger I get a 422 response from Basecamp saying the <title> attribute is empty which it isn't

<?xml version="1.0" encoding="UTF-8"?> <errors> <error>Title can't be blank</error> </errors>

I have tried the same POST via the 'Advanced Rest Client' in Chrome and the post is successful.

function postBasecampApi(endpoint, payload) {

  var xml = ""+
    '<request>'+
      '<post>'+
        '<category-id>123890662</category-id>'+
          '<title>sting</title>'+
            '<body>testing</body>'+
              '</post>'+
                '<notify>test notify</notify>'+
                  '</request>';

  var url = basecampCompleteUrl + '/projects/12029591/posts.xml';  

  var headers = {
    Authorization : 'Basic ' +  Utilities.base64Encode(user + ':' + password)
  }
  var opt = {
    'method': 'POST',
    'payload': xml,
    'headers' : headers
  };

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

  return Xml.parse(response.getContentText(), false);
}

Thanks in advance for any help

4

1 回答 1

0

发现问题了,fetch Content-Type 默认不是'application/xml'。我在 opt 对象中添加了以下内容,一切正常

var opt = {
  'contentType': 'application/xml',
  'method': 'POST',
  'payload': xml,
  'headers' : headers
};

Content-Type添加到标题中是不值得的

于 2014-04-10T14:30:09.827 回答