简体   繁体   中英

How to run a SOAP request in NodeJs?

It might sound like a repeated question at first, but I've gone through all the blogs/ tutorials/ videos I found but none of them actually says how you run that request. Example: for a RESTful request, you code in NodeJs, hit the route(https://localhost/3000/api/getStudent) and get the response. In code you use router.post('/getStudent', async (req,res) => { // Check response here or manipulate })

But in soap after coding all the functions like in here-

  1. https://medium.com/metrosystemsro/with-node-js-wrap-backend-soap-webservices-in-a-restful-api-a96887575046
  2. https://dafabulousteach.wpcomstaging.com/2016/05/19/making-a-soap-call-with-node/
  3. https://betterprogramming.pub/how-to-perform-soap-requests-with-node-js-4a9627070eb6

Where do I call the the function and how do I pass parameters and test it? And how do I check the response?

To place this in the context of what you are asking about, a SOAP service is just a server listening on an address for POST requests that have an XML payload. That's it.

So if your SOAP web service address is http://example.com/service_endpoint then you can call the web service by making a POST HTTP request at this address and sending it a SOAP XML message as payload.

Obviously, the XML message in the request must match something that the service expects and you know how to build that XML either by reading documentation or by using the WSDL of the SOAP web service.

So if you know how to make a POST HTTP request to a REST service address and send it a XML payload (although most commonly for REST you use JSON), you already know how to call a SOAP web service.

Now, for convenience, because SOAP is a protocol, the way you call the service and what it expects as XML payload is described by the WSDL, and you can use tools that read the WSDL and create a client API that you can invoke like any other method in your code. The tools handle the details of the HTTP POST request for you, and also the marshalling of any parameters to XML. This is probably what you found confusing.

So let's take an example.

If you have, say, a service that has an operation named savePerson which accepts firstName and lastName as parameters and is described in the WSDL, then your tooling might generate a client that has such a method and accepts a Person object and you can call it like:

var response = client.savePerson({ "firstName": "Kim", "lastName": "Seokjin" });

or some variation of this. You then get a response that you can read just like any other object. You might get a promise instead, or an event, or whatever way the client chooses for how to work. When you use this code, what happens is that the client performs a HTTP POST request behind the scenes for you and marshals the person object to XML, something like this:

POST /service_endpoint
Host: http://example.com
Content-Type: text/xml; charset="utf-8"
Content-Length: nnnn

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
 <SOAP-ENV:Body>
  <savePerson>
   <person>
     <firstName>Kim</firstName>
     <lastName>Seokjin</lastName>
   </person>
  </savePerson> 
 </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

You can obviously send this request yourself with whatever HTTP library you prefer if you don't want to generate a client from the WSDL. But people prefer to use a generated client instead of dealing with these details directly (ie making HTTP requests, parsing XML, etc).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM