0

I am trying to access an API though Google Apps Script. However, I am not sure if I can or not and was wondering if I could get some feedback on if this is possible or not. I am able to GET data from their API, but not POST updates through their API.
Here are the requirements:

  • Construct and parse valid XML documents.
  • Programmatically establish an SSL connection.
  • Programmatically submit XML documents to a secure URL.
  • Securely store an account ID authentication string to be accessed by the code that submits the request. The application does not provide third parties with any software to install. All integrations are purely based on the exchange of valid XML documents.

I know I can construct and parse valid XML in Apps Script, but I am not sure about submitting an XML doc to to a secure URL or the other points.

I have been trying to get this to work for about 5 hours, without any success and so I wanted to check and see if these technical requirements are out of the scope of GAS.

--Update-- Here is my code update:

function createXML() {
var token = 'API Token';
var root = XmlService.createElement('MemoSubmissionRequest').setAttribute('version', '1.00');
var udiChild = XmlService.createElement('UDIParameter');
root.addContent(udiChild);
var paramterKey = XmlService.createElement('Parameter').setAttribute('Key', 'UDIAuthToken').setText(token); 
udiChild.addContent(paramterKey);
var memoChild = XmlService.createElement('Memo');
root.addContent(memoChild);
var orderNumberChild = XmlService.createElement('OrderNumber').setText('454655');
memoChild.addContent(orderNumberChild);
var memoTextChild = XmlService.createElement('MemoText').setText('Test MEMO');
memoChild.addContent(memoTextChild);

var document = XmlService.createDocument(root);
var xml = XmlService.getPrettyFormat().format(document);
return xml;
}

function xmlPost() {

 var token = 'API Token';
  var url = 'https://api.omx.ordermotion.com/hdde/xml/udi.asp?UDIAuthToken=' + token;
  var xml = createXML();
  Logger.log(xml);
 // var xmlDoc = XmlService.parse(xml);
  var xmlLength = xml.length 
  var headers = {
    'Content-Type': 'text/xml; charset=uft-8',
  };
  var options = {
    "method": 'POST',
    "headers":headers,
    "payload": xml,
    'Content-length': xmlLength,
  };
  Logger.log(url);
  var response= UrlFetchApp.fetch(url,options).getContentText();
  Logger.log(response);
}

There Error message I am getting now is: Request failed for https://api.omx.ordermotion.com/hdde/xml/udi.asp?

I am not really sure what else to try now. Any feedback is welcome.

Bokai
  • 107
  • 3
  • 17
  • Did you see [this SO question](https://stackoverflow.com/questions/26615546/google-apps-script-urlfetchapp-post-file)? I think it does what you're asking or at least something similar. – Casper Jun 21 '17 at 08:50
  • @Casper I did not see that post yet! Thanks so much for sharing it. I am trying that now. – Bokai Jun 21 '17 at 10:31

0 Answers0