Question

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

Was it helpful?

Solution

Found the problem, the fetch Content-Type default is not 'application/xml'. I added the following to the opt object and everything worked fine

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

It is worth nothing adding Content-Type to the headers does not work

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top