Request using xml+soap in asmx api

Enonic version: 6.12


I have to use a asmx api that use soap. Can i do that using the funtion request from http-client lib?

1 Like

Yes, we have used the http-client lib with a SOAP interface in several projects, so it’s definitely possible.

1 Like

Could you please give a example to how are you doing that. I tried to use the http-client to request to a asmx API that use soap and the response is returning the body null. But when i do this same request in Postman the response body return correctly as expected in API.

Hi,

Anyone would have an example that could help us please?

Below it is the code we are using, (I set a test domain in the example. We tried the same post request in postman, and it works correctly. But when we try to call in from enonic, the reponse object comes with status 200(Ok), but the body comes null.

var xmlInfo = ‘<?xml version="1.0" encoding="utf-8"?>’ +
‘’ +
‘’ +
‘’ +
‘’+ params.Token +’’ +
‘’+ params.ActNo +’’ +
‘’ +
‘’ +
‘’;

var response = libs.httpClientLib.request(
{
    url: requestUrl,
    headers: {
        'Content-Type': 'application/soap+xml; charset=utf-8',
    },
    method: 'POST',
    body: xmlInfo,
});

This is the response:

{
“status”: 200,
“message”: “OK”,
“contentType”: “application/soap+xml; charset=utf-8”,
“headers”: {
“Cache-Control”: “private, max-age=0”,
“Content-Length”: “673”,
“Content-Type”: “application/soap+xml; charset=utf-8”,
“Date”: “Tue, 02 Jan 2018 12:49:30 GMT”,
“Set-Cookie”: “NSC_104840_O13_GPDVT_MCWT=ffffffff094acee545525d5f4f58455e445a4a423660;expires=Tue, 02-Jan-2018 12:51:31 GMT;path=/;httponly”
}
}

Have anyone seen anything like this?

And would you have an example on how you do a post request for a SOAP service?

Hi,

I would log the exact request being sent from XP, and then compare that to the one sent from Postman. Maybe there’s some headers missing or not the same? Can you post the data Postman sends and that works?

Hi

I think the response content-type (“application/soap+xml”) might be causing the problem.

The response object returned by the function has 2 properties for the body.
One is meant for text: response.body, the other one for binary: response.bodyStream.

If the http-client lib detects that the response is of type “text” it sets the body response with the text. This is to avoid encoding big chunks of binary data as strings, when they won’t be used.

Looks like this is not working in this case, because we assumed "application/*" would not be a content type for text. I will create an issue to fix it in the library.

As a workaround, try using the bodyStream property and convert it to string. You need to use the io library, something like this:

var ioLib = require('/lib/xp/io');

...

var response = libs.httpClientLib.request(
{
    url: requestUrl,
    headers: {
        'Content-Type': 'application/soap+xml; charset=utf-8',
    },
    method: 'POST',
    body: xmlInfo,
});

var body = ioLib.readText(response.bodyStream); // <- XML string from the response



1 Like

It works. Thank you.

We have just released a new version of http-client library with this bugfix.
If you update your build.gradle dependency to version 1.0.1 it should work without having to use the io library.

include 'com/enonic/lib:lib-http-client:1.0.1'

2 Likes