在Google Apps Script中调用SAP SOAP Web服务遇内容类型不支持错误求助
Hey Stefano, let's break down what's going wrong with your SOAP service call and fix it up!
Key Issues Identified
1. You're targeting the WSDL file instead of the actual service endpoint
Your webservice URL ends with .wsdl — this points to the service description file, not the endpoint that accepts SOAP requests. SAP SOAP endpoints use the base URL without the .wsdl suffix.
2. Request options are incorrectly structured
In your options object, you've nested method, contentType, and payload inside the headers object — these are top-level properties, not header values. This means your request is defaulting to GET (instead of POST), and the contentType isn't being set properly, which triggers that "Unsupported Content Type" error.
Corrected Code
Here's the fixed version with explanations of the changes:
var user = 'user'; var pwd = 'pwd'; // Remove the .wsdl suffix to target the actual service endpoint var webservice = 'http://xx.xxx.xx.xx:xxxx/sap/bc/srt/wsdl/flv_10002A111AD1/bndg_url/sap/bc/srt/rfc/sap/zdemo_google_ws/100/zdemo_google_ws/zdemo_google_ws?sap-client=100'; function readData() { // Clean up anything currently on the spreadsheet SpreadsheetApp.getActiveSheet().clear(); try { // SOAP envelope (unchanged, assuming namespace matches your SAP service) var xml = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:sap-com:document:sap:soap:functions:mc-style\">" + "<soapenv:Header/>" + " <soapenv:Body>" + " <urn:ZDemoGoogle>" + " <ItWebs>" + " <item>" + " <Protocol></Protocol>" + " <Tasktype></Tasktype>" + " <EntryId></EntryId>" + " <Account></Account>" + " <Mandt></Mandt>" + " <Host></Host>" + " <Port></Port>" + " <Destination></Destination>" + " <Path></Path>" + " <Counter></Counter>" + " <Calltime></Calltime>" + " <ExecutionTi></ExecutionTi>" + " <DataSend></DataSend>" + " <DataSendTi></DataSendTi>" + " <DataReceive></DataReceive>" + " <DataReceiveTi></DataReceiveTi>" + " </item>" + " </ItWebs>" + " </urn:ZDemoGoogle>" + " </soapenv:Body>" + "</soapenv:Envelope>"; // Fixed options: method, contentType, payload are top-level properties var options = { method: "post", contentType: "text/xml", payload: xml, headers: { "Authorization": "Basic " + Utilities.base64Encode(user + ':' + pwd) }, muteHttpExceptions: true }; // Fetch from the corrected service endpoint var response = UrlFetchApp.fetch(webservice, options); // Add logic here to process the response (e.g., parse XML and write to sheet) Logger.log(response.getContentText()); } catch (e) { Logger.log("Error: " + e.toString()); } }
Additional Checks
- Double-check that your SAP service endpoint URL is correct (you can verify this from the WSDL file's
<soap:address location="...">element) - Ensure the SOAP envelope's namespace (
urn:sap-com:document:sap:soap:functions:mc-style) matches the one defined in your SAP service - If you still get authentication errors, confirm the
userandpwdhave the necessary permissions to call the SAP Web service
内容的提问来源于stack exchange,提问作者Stefano Simion

