Hello,
I have the following code and it throws an error when run, any suggestions that might help resolve the error?
Thanks in advance
Error: NavHttpClient Request Failed
Code:
codeunit 50401 ImportXMLMessage
{
procedure ImportXMLMessage()
var
SOAPAction: Record SOAPAction;
OutStrm: OutStream;
InStrm: InStream;
DialogTitle: Label 'Upload file...';
TempFileName: Text;
RequestHeaders: HttpHeaders;
RequestMessage: HttpRequestMessage;
RequestContent: HttpContent;
Client: HttpClient;
ResponseMessage: HttpResponseMessage;
Content: HttpContent;
XML: Text;
begin
Clear(SOAPAction.XMLMessage);
if not SOAPAction.XMLMessage.HasValue then begin
file.UploadIntoStream(DialogTitle, '', 'XML file(*.xml)|*.xml', TempFileName, InStrm);//then begin
Content.Clear();
RequestContent.Clear();
RequestHeaders.Clear();
RequestMessage.Content().Clear();
ResponseMessage.Content().Clear();
ResponseMessage.Headers().Clear();
RequestContent.WriteFrom(InStrm);
RequestContent.GetHeaders(RequestHeaders);
RequestHeaders.Remove('Content-Type');
RequestHeaders.Add('Content-Type', 'text/xml;charset=utf-8');
RequestHeaders.Add('SOAPAction', 'GetMovimentacao');
RequestMessage.Content := RequestContent;
RequestMessage.Method := 'POST';
RequestMessage.SetRequestUri('
https://uhc.unihealth.com.br:3132/Interface.asmx');
RequestMessage.Content(RequestContent);
if Client.Send(RequestMessage, ResponseMessage) then begin
ResponseMessage.Content().ReadAs(XML);
Message(XML);
end else begin
Error('Error:\' +
'Status Code : %1\' +
'Description: %2',
ResponseMessage.HttpStatusCode,
ResponseMessage.ReasonPhrase);
end;
end;
}
Answers
I use this procedure to call several SOAP WS and get back an XML documetnt with the response, you could try it and let me know if it works for you.
Regards
I tried with the suggested code, honestly I didn't make a lot of modifications as it is similar to mine, but it still has the same error. I'm not sure if it is presented by URL or procedure. Is it possible that you can share some code that works with real URLs so that I can see the procedure?
You can use this site to test the SOAP WS call.
https://www.crcind.com/csp/samples/SOAP.Demo.CLS
Regards
I must be doing something wrong since even with the test site it sends me the same error. I used the procedure below
procedure LoadXMLandCallWS()
var
InStr: InStream;
DialogTitle: Label 'Subir archivo...';
TempFileName: Text;
RequestHeaders: HttpHeaders;
RequestMessage: HttpRequestMessage;
RequestContent: HttpContent;
Client: HttpClient;
ResponseMessage: HttpResponseMessage;
XML: Text;
begin
file.UploadIntoStream(DialogTitle, '', 'XML file(*.xml)|*.xml', TempFileName, InStr);
RequestContent.WriteFrom(InStr);
RequestHeaders.Clear();
RequestContent.GetHeaders(RequestHeaders);
RequestHeaders.Remove('Content-Type');
RequestHeaders.Add('Content-Type', 'text/xml; charset=UTF-8');
RequestHeaders.Add('SOAPAction', 'FindPerson');
RequestMessage.SetRequestUri('https://www.crcind.com/csp/samples/SOAP.Demo.CLS');
RequestMessage.Method('POST');
if Client.Send(RequestMessage, ResponseMessage) then begin
ResponseMessage.Content().ReadAs(XML);
Message(XML);
end else begin
Error('Error: \' +
'Status Code: %1\' +
'Description: %2',
ResponseMessage.HttpStatusCode,
ResponseMessage.ReasonPhrase);
end;
end;
}
Below is stored in InStream
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:s="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<FindPersonResponse xmlns="http://tempuri.org"/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I have added this line to you code, marked with "// <<
This line is missing"
RequestMessage.Content(RequestContent);
The input XML is this one:
And i got this response:
Regards
Hello @ftornero
Thanks for your reply, that fixes the problem. However, I still have not managed to make it work in the URL I want, I do not know if it is because in the content of the XML it has lines with username and password, and that in turn must be defined in the header. The content of the XML is this.
If the content of the XML has credentials, is it necessary to add something else to the structure of the procedure?
That depends of the WS that you are calling.
Don't you have any documentation about the WS, the WS description , a WSDL file, etc. ?
Regards
I did a test in Postman and if it gave me an answer, I looked into the code options and noticed that there is a line that adds the host, I tried to add it in the structure of the AL code, however it sends an error.
These are the lines I tried to add:
This is the error that Business Central sends:
Misused header name. Make sure request headers are used with HttpRequestMessage, response headers with HttpResponseMessage, and content headers with HttpContent objects.
Do you know how to add the host to the header or how should I add the host?
I don't think that header is necessary because usually is provided for the Http client and get from the URL
The answer that you get from Postman is the expected one ?
Regards
That's right, in Postman I get the correct answer
Could you post the data with the Postman call, hiding the sensitive information ?
Regards
Well, the XML is pretty simple and that must works from BC without any issue.
If you want, we could connect and I take a look at your code.
Regards
If you have the opportunity to connect, I would appreciate it. Please tell me what time is convenient for you and through which tool
I just sent to you a PM to connect with you.
Regards
I don't know if you have solve the conection problem with de remote WS.
Here is some informatio about the IP range en BC SaaS
https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/faq#which-ip-addresses-or-ranges-does-my-environments-api-use
and a post about the same problem wuth the IP white list the some WS.
https://community.dynamics.com/business/f/dynamics-365-business-central-forum/276553/can-we-set-up-a-static-ip-address-to-dynamics-365-business-central-on-cloud
And a little App to show the client IP address:
Regards
I have already confirmed the reason for the error, this is due to the restriction of the server where the web service is hosted. I tried the code in the locale and it worked fine there, I got the expected result. Regarding access for cloud environment, IP range will be whitelist, I hope that will solve the problem.
I did test the code to get the IP address, but from what I read in the link, BC's IP is dynamic so if it is whitelist the IP might change and an error would occur.