在API请求中添加XML参数

时间:2018-10-17 15:21:51

标签: xml api soap google-apps-script

使用SoapUI之前,我一直存在错误,直到我更正了参数(现在我得到了所需的响应)。使用Apps Script,我正在为同样的错误而苦苦挣扎,但似乎无法正确添加XML参数,下面的示例是我尝试的最后一种方法。

根据API文档:

"Wrap the following XML structure in a variable called 'callmeasurement_xml_input' "
<?xml version="1.0" encoding="UTF-8"?>
<callmeasurement username="abc" password="123" api="1"/>

SoapUI中起作用的“参数”是:

?callmeasurement_xml_input=<?xml version="1.0" encoding="utf-16"?><callmeasurement username="abc" password="123" api="1111"/>

这是我当前(无法使用)的AppsScript:

function apiRequest(){

  var url = 'http://api.callmeasurement.com/api/dnis_list.cfm';

  var rawXML = '<callmeasurement_xml_input>'
  + '<?xml version="1.0" encoding="utf-16"?>'
  + '<callmeasurement username="abc" password="123" api="1111"/>'
  + '</callmeasurement_xml_input>';

  var options = {
    'contentType': 'application/xml',
    'method': 'POST',
    'payload': rawXML
  };
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
  // Returns an error
}

1 个答案:

答案 0 :(得分:0)

应该将XML编码并传递到callmeasurement_xml_input查询参数中,而不是将XML包装在callmeasurement_xml_input元素中并将其发布到有效负载中。

function apiRequest(){

  var rawXML = '<?xml version="1.0" encoding="utf-16"?>'
  + '<callmeasurement username="abc" password="123" api="1111"/>';

  var url = 'http://api.callmeasurement.com/api/dnis_list.cfm'
  + '?callmeasurement_xml_input=' + encodeURIComponent(rawXML);

  var options = {'method': 'POST'};
  var response = UrlFetchApp.fetch(url, options);
  Logger.log(response.getContentText());
}