BC365 - Error calling Web Service

isabtogumonisabtogumon Member Posts: 10
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

  • ftorneroftornero Member Posts: 348
    Hello @isabtogumon,

    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.
        local procedure CallPostWS(InS: InStream; URL: Text; SOAPAction: Text; var ErrorMsg: Text; var xmlDoc: XmlDocument): Boolean
        var
            Client: HttpClient;
            RequestContent: HttpContent;
            RequestHeader: HttpHeaders;
            RequestMsg: HttpRequestMessage;
            ResponseMSg: HttpResponseMessage;
            ResTxt: Text;
    
        begin
            // Add content
            RequestContent.WriteFrom(InS);
    
            // Add headers
            RequestHeader.Clear();
            RequestContent.GetHeaders(RequestHeader);
            RequestHeader.Remove('Content-Type');
            RequestHeader.Add('Content-Type', 'text/xml; charset=UTF-8');
            if SOAPAction <> '' then
                RequestHeader.Add('SOAPAction', SOAPAction);
            RequestMsg.Content(RequestContent);
    
            // Set URL and Method
            RequestMsg.SetRequestUri(URL);
            RequestMsg.Method('POST');
    
            if Client.Send(RequestMsg, ResponseMSg) then begin
                if ResponseMSg.IsSuccessStatusCode() then begin
                    ResponseMSg.Content.ReadAs(ResTxt);
                    if XmlDocument.ReadFrom(ResTxt, XMLDoc) then begin
                        ErrorMsg := '';
                        exit(true);
                    end else begin
                        ErrorMsg := ResTxt;
                        exit(false);
                    end;
                end else begin
                    ErrorMsg := ResponseMSg.ReasonPhrase();
                    exit(false);
                end;
            end else begin
                if ResponseMSg.Content.ReadAs(ResTxt) then
                    ErrorMsg := ResTxt;
                exit(false);
            end;
        end;
    

    Regards
  • isabtogumonisabtogumon Member Posts: 10
    Hello @ftornero and thanks,

    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?
  • ftorneroftornero Member Posts: 348
    Hello @isabtogumon,

    You can use this site to test the SOAP WS call.

    https://www.crcind.com/csp/samples/SOAP.Demo.CLS

    Regards
  • isabtogumonisabtogumon Member Posts: 10
    Hello @ftornero and thanks again

    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/&quot; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:s="http://www.w3.org/2001/XMLSchema"&gt;
    <SOAP-ENV:Body>
    <FindPersonResponse xmlns="http://tempuri.org"/&gt;
    </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
  • ftorneroftornero Member Posts: 348
    Hello @isabtogumon

    I have added this line to you code, marked with "// <<
    This line is missing"

    RequestMessage.Content(RequestContent);
        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.Content(RequestContent);  // <<------ This line is missing
            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;
    

    The input XML is this one:
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org">
       <soapenv:Header/>
       <soapenv:Body>
          <tem:FindPerson>
             <tem:id>1</tem:id>
          </tem:FindPerson>
       </soapenv:Body>
    </soapenv:Envelope>
    

    And i got this response:

    wcaa6df1mx6x.png

    Regards
  • isabtogumonisabtogumon Member Posts: 10

    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.

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <GetMovimentacao xmlns="http://www.unihealth.com.br/Unihealth">
          <Credencial>
            <UserName>Username</UserName>
            <Password>Password</Password>
          </Credencial>
        </GetMovimentacao>
      </soap:Body>
    </soap:Envelope>
    

    If the content of the XML has credentials, is it necessary to add something else to the structure of the procedure?
  • ftorneroftornero Member Posts: 348
    Hello @isabtogumon,

    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
Sign In or Register to comment.