Search code examples
javascriptgoogle-apps-scriptsoapxmlhttprequestsalesforce

Premature end of file error in XML Request


for some reason I am getting this error when trying to create a simple login request to Salesforce.

This works exactly as it should in postman, however, when I try and convert to Google Apps Script it seems to create a "Premature end of file" error.


  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>[email protected]</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())```




Solution

  • How about this modification?

    Modification points:

    • I think that in this case, Cookie: "BrowserId=sB-PspOVEeqEttdm-K56tw" might not be required.
    • The properties of body and redirect are not included in the arguments of UrlFetchApp.fetch.

    Modified script:

    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>[email protected]</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())
    

    Note:

    • In this modification, it supposes that the values of SOAPAction, raw and https://test.salesforce.com/services/Soap/c/48.0/ are correct.

    Reference: