下面是我正在使用的Google应用程序脚本SOAP API信封调用。它可以正常连接,并且您可以在下面看到它返回响应,但是我的响应已编码。如何/在何处添加一点以进行Base64解码以查看返回的XML而不是字符串?我对SOAP API完全陌生,对应用程序脚本还是新手。谢谢!
function getData() {
var webservice = ‘https://someplace.com/services/stuff/’;
var xml = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://someurlhere.com">'
+ '<soapenv:Header/>'
+ '<soapenv:Body>'
+ '<ser:getReportXML soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> '
+ '<in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + userid + '</in0> '
+ '<in1 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + password + '</in1> '
+ '<in2 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + startDate + '</in2> '
+ '<in3 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + endDate + '</in3> '
+ '</ser:getReportXML> '
+ '</soapenv:Body>'
+' </soapenv:Envelope>'
var options = {
headers: {"SOAPAction" :"https://someplace.com/services/stuff/"},
method: "post",
contentType: "text/xml",
payload: xml,
muteHttpExceptions: true,
};
var serviceaddress = webservice;
var response = UrlFetchApp.fetch(serviceaddress, options);
Logger.log(response);
};
它在响应中返回一个编码字符串,但是我想查看实际的XML结果:
[19-01-31 11:46:02:122 PST] <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getReportXMLResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1=" http://someurlhere.com "><getReportXMLReturn xsi:type="soapenc:base64Binary" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">WMui8xyxcPQXmZgSerdPd94bwWxGsAMgdmVyc2lvbj0iFRxlTSerdgiPz4NCg0KPQFET0NUW
我正在尝试使响应输出看起来像xml而不是一串字符
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ETRANS PUBLIC "-//Something//ethings DTD//EN" "https://www.url.com/dtd/ethings_1_0.dtd">
<ETRANS>
<USER ID="AABB1122" USER_NAME="Smith, John" DATE="2019-02-01 09:41:45" DEPT_ID=""/>
</ETRANS>
所以我发现我的响应确实具有XML的编码主体,但是在实际编码的数据之前,响应中还具有所有这些额外的位,因此解码器失败了,因为它不知道如何处理响应开头显示在此处
[19-01-31 11:46:02:122 PST] <?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:getReportXMLResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1=" http://someurlhere.com "><getReportXMLReturn xsi:type="soapenc:base64Binary" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
,以及底部的一些其他位,类似于上述位的结尾。我是否需要传递某些请求,以使SOAP请求仅返回字符串,而不是这些多余的位(看起来像是在将“信封”放在要发送回的编码数据周围)?
答案 0 :(得分:0)
我终于想通了。我不知道是否有一种“更好”的方式来做到这一点,但是如果您看到我所做的,也许您可能会分享这种“更好”的方式。所以我在下面添加了“ NEW BIT”行。为了解码base64位,我不得不放弃SOAP Envelop。我可以在Google Apps脚本中弄清楚的唯一方法是将响应另存为文件,因为这似乎在“神奇地”消除了SOAP。因此,我必须获取我创建的MyTestFile文件中剩下的base64位,并将其解码为文本字符串,然后解码/转换它,然后创建了我要查找的xml。我希望这可以帮助其他人。
function getData() {
var webservice = ‘https://someplace.com/services/stuff/’;
var newDocName = 'MyTestFile'
var xml = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://someurlhere.com">'
+ '<soapenv:Header/>'
+ '<soapenv:Body>'
+ '<ser:getReportXML soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> '
+ '<in0 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + userid + '</in0> '
+ '<in1 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + password + '</in1> '
+ '<in2 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + startDate + '</in2> '
+ '<in3 xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">' + endDate + '</in3> '
+ '</ser:getReportXML> '
+ '</soapenv:Body>'
+' </soapenv:Envelope>'
var options = {
headers: {"SOAPAction" :""},
method: "post",
contentType: "text/xml",
payload: xml,
muteHttpExceptions: true,
};
var serviceaddress = webservice;
var response = UrlFetchApp.fetch(serviceaddress, options).getContentText();
//Logger.log(response);
//NEW BIT BEGINS HERE THAT DECODED THE RESPONSE
var blob = DriveApp.createFile('dummy',response, 'text/html').getBlob();
var resource = {
title: newDocName,
convert: true,
mimeType: 'application/vnd.google-apps.file'
};
var file = Drive.Files.insert(resource,blob);
var doc = DocumentApp.openById(file.id);
var text = doc.getBody().getText();
var decoded = Utilities.base64Decode(text,Utilities.Charset.UTF_8); // This was a byte array
var decodedstr = Utilities.newBlob(decoded).getDataAsString() // This was the xml I was looking for
Logger.log(decodedstr);
//NEW BIT ENDS HERE
};