如何通过Node.js调用SOAP Web服务?请求定义错误求解决
Hey there! Let's troubleshoot that "request not defined" error you're hitting. Since you can successfully call the service via SOAP UI and Postman, the issue almost certainly lies in how you're structuring the parameters in your Node.js code. Here's what to do:
1. Fix Parameter Name Mismatch First
You mentioned the WSDL defines the required parameters as user and password, but your code uses pass instead of password. That's a common small mistake—start by correcting that key name to match the WSDL definition exactly.
2. Inspect the Service's Expected Parameter Structure
SOAP services often expect parameters to be nested within a parent object tied to the operation's request schema (like a fetchProductsRequest or fetchProductsIn wrapper). The soap package needs this structure to generate the correct XML request body.
To see exactly what the service expects, add a log of the client's description right after creating it:
const soap = require('soap'); const url = 'http://ws.dev.freepaid.co.za/airtimeplus/?wsdl'; soap.createClient(url, function(err, client) { if (err) { console.error('Error creating SOAP client:', err); return; } // Log the full service structure to check parameter requirements console.log('Service Description:', client.describe()); // Adjust args based on the describe output (example structure) const args = { request: { user: 'your_username', password: 'your_password' } }; client.fetchProducts(args, function(err, result) { if(err) console.log("Error:", err) else console.log("Result:", result); }) });
Run this with node server.js and look for the fetchProducts section in the logged output. It will explicitly show you the required key names and nesting hierarchy for your parameters.
3. Align with SOAP UI's Request Structure
If you want a quicker reference, check the raw XML request SOAP UI sends when you make a successful call. For example, if the SOAP UI request looks like this:
<soapenv:Body> <tns:fetchProducts> <tns:request> <tns:user>your_username</tns:user> <tns:password>your_password</tns:password> </tns:request> </tns:fetchProducts> </soapenv:Body>
Then your args object should mirror that nesting exactly: { request: { user: '...', password: '...' } }.
4. Test the Corrected Code
Once you've adjusted the args to match the service's expected structure and parameter names, re-run your code. This should resolve the "request not defined" error, as your SOAP request will now match the format the service accepts (just like your working SOAP UI/Postman calls).
内容的提问来源于stack exchange,提问作者Byron Jehoma

