XML请求中的文件过早结束错误

时间:2020-05-19 15:54:21

标签: javascript google-apps-script soap xmlhttprequest salesforce

由于某些原因,当我尝试向Salesforce创建简单的登录请求时遇到此错误。

这与邮递员中的工作完全一样,但是,当我尝试转换为Google Apps脚本时,似乎会出现“文件过早结束”错误。


  var myHeaders = {

    "Content-Type": 'text/xml',
    SOAPAction: '/',
    Cookie: "BrowserId=sB-PspOVEeqEttdm-K56tw",


  }



/*myHeaders.append("Content-Type", "text/xml");
myHeaders.append("SOAPAction", "\"\"");
myHeaders.append("Cookie", "BrowserId=sB-PspOVEeqEttdm-K56tw");*/


var raw = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <Header>\n    </Header>\n    <Body>\n        <login xmlns=\"urn:enterprise.soap.sforce.com\">\n            <username>xx@xx.com</username>\n            <password>xx</password>\n        </login>\n    </Body>\n</Envelope>";
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow',
  muteHttpExceptions : true
};

var response = UrlFetchApp.fetch("https://test.salesforce.com/services/Soap/c/48.0/", requestOptions)
  //.then(response => response.text())
  //.then(result => logger.log(result))
  //.catch(error => logger.log('error', error)); 


Logger.log(response.getContentText())```



1 个答案:

答案 0 :(得分:2)

该修改如何?

修改点:

  • 我认为在这种情况下,可能不需要Cookie: "BrowserId=sB-PspOVEeqEttdm-K56tw"
  • bodyredirect的属性未包含在UrlFetchApp.fetch的参数中。

修改后的脚本:

var myHeaders = {
  SOAPAction: '/',
}
var raw = "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">\n    <Header>\n    </Header>\n    <Body>\n        <login xmlns=\"urn:enterprise.soap.sforce.com\">\n            <username>xx@xx.com</username>\n            <password>xx</password>\n        </login>\n    </Body>\n</Envelope>";
var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  contentType: "text/xml",
  payload: raw,
  muteHttpExceptions : true
};
var response = UrlFetchApp.fetch("https://test.salesforce.com/services/Soap/c/48.0/", requestOptions);
Logger.log(response.getContentText())

注意:

  • 在此修改中,它假设SOAPActionrawhttps://test.salesforce.com/services/Soap/c/48.0/的值是正确的。

参考: