Consume external soap Webservice from SaaS Business Central

weneedweneed Member Posts: 51
Is there any way to do this?
All procedure are marked [Scope('OnPrem')]..

Best Answer

  • weneedweneed Member Posts: 51
    Answer ✓
    ftornero wrote: »
    Hello @weneed,

    Yes, you can use something like this to send an XML Ffle to and SOAP WS and get the response:
    procedure Send(xmlDoc: XmlDocument; URLDest: Text; var ErrorCode: Text; var xmlDoc2: XmlDocument): Boolean
    var
    	URL: Text;
    	Client: HttpClient;
    	Content: HttpContent;
    	RequestMsg: HttpRequestMessage;
    	ResponseMSg: HttpResponseMessage;
    	RequestHeader: HttpHeaders;
    	ContentHeader: HttpHeaders;
    	ReqTxt: Text;
    	ResTxt: Text;
    
    begin    
    	URL := URLDest;
    
    	RequestHeader.Clear();
    	RequestMsg.GetHeaders(RequestHeader);
    	RequestHeader.Add('Accept', 'application/xml');
    
    	// Add XML to content
    	XmlDoc.WriteTo(ReqTxt);
    	Content.WriteFrom(ReqTxt);
    
    	ContentHeader.Clear();
    	Content.GetHeaders(ContentHeader);
    	ContentHeader.Remove('Content-Type');
    	ContentHeader.Add('Content-Type', 'text/xml');
    	RequestMsg.Content(Content);
    
    	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, XMLDoc2) then begin
    			   ErrorCode := '';
    				exit(true);
    			end else begin
    				ErrorCode := ResTxt;
    				exit(false);
    			end;
    		end else begin
    			ErrorCode := ResponseMSg.ReasonPhrase();
    			exit(false);
    		end;
    	end else begin
    		if ResponseMSg.Content.ReadAs(ResTxt) then
    			ErrorCode := ResTxt
    		else
    			ErrorCode := ResponseMSg.ReasonPhrase;
    		exit(false);
    	end;
    end;
    

    Regards

    Thanks. There is a way to authenticate giving username and password?

Answers

  • ftorneroftornero Member Posts: 459
    Hello @weneed,

    Yes, you can use something like this to send an XML Ffle to and SOAP WS and get the response:
    procedure Send(xmlDoc: XmlDocument; URLDest: Text; var ErrorCode: Text; var xmlDoc2: XmlDocument): Boolean
    var
    	URL: Text;
    	Client: HttpClient;
    	Content: HttpContent;
    	RequestMsg: HttpRequestMessage;
    	ResponseMSg: HttpResponseMessage;
    	RequestHeader: HttpHeaders;
    	ContentHeader: HttpHeaders;
    	ReqTxt: Text;
    	ResTxt: Text;
    
    begin    
    	URL := URLDest;
    
    	RequestHeader.Clear();
    	RequestMsg.GetHeaders(RequestHeader);
    	RequestHeader.Add('Accept', 'application/xml');
    
    	// Add XML to content
    	XmlDoc.WriteTo(ReqTxt);
    	Content.WriteFrom(ReqTxt);
    
    	ContentHeader.Clear();
    	Content.GetHeaders(ContentHeader);
    	ContentHeader.Remove('Content-Type');
    	ContentHeader.Add('Content-Type', 'text/xml');
    	RequestMsg.Content(Content);
    
    	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, XMLDoc2) then begin
    			   ErrorCode := '';
    				exit(true);
    			end else begin
    				ErrorCode := ResTxt;
    				exit(false);
    			end;
    		end else begin
    			ErrorCode := ResponseMSg.ReasonPhrase();
    			exit(false);
    		end;
    	end else begin
    		if ResponseMSg.Content.ReadAs(ResTxt) then
    			ErrorCode := ResTxt
    		else
    			ErrorCode := ResponseMSg.ReasonPhrase;
    		exit(false);
    	end;
    end;
    

    Regards
  • weneedweneed Member Posts: 51
    Answer ✓
    ftornero wrote: »
    Hello @weneed,

    Yes, you can use something like this to send an XML Ffle to and SOAP WS and get the response:
    procedure Send(xmlDoc: XmlDocument; URLDest: Text; var ErrorCode: Text; var xmlDoc2: XmlDocument): Boolean
    var
    	URL: Text;
    	Client: HttpClient;
    	Content: HttpContent;
    	RequestMsg: HttpRequestMessage;
    	ResponseMSg: HttpResponseMessage;
    	RequestHeader: HttpHeaders;
    	ContentHeader: HttpHeaders;
    	ReqTxt: Text;
    	ResTxt: Text;
    
    begin    
    	URL := URLDest;
    
    	RequestHeader.Clear();
    	RequestMsg.GetHeaders(RequestHeader);
    	RequestHeader.Add('Accept', 'application/xml');
    
    	// Add XML to content
    	XmlDoc.WriteTo(ReqTxt);
    	Content.WriteFrom(ReqTxt);
    
    	ContentHeader.Clear();
    	Content.GetHeaders(ContentHeader);
    	ContentHeader.Remove('Content-Type');
    	ContentHeader.Add('Content-Type', 'text/xml');
    	RequestMsg.Content(Content);
    
    	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, XMLDoc2) then begin
    			   ErrorCode := '';
    				exit(true);
    			end else begin
    				ErrorCode := ResTxt;
    				exit(false);
    			end;
    		end else begin
    			ErrorCode := ResponseMSg.ReasonPhrase();
    			exit(false);
    		end;
    	end else begin
    		if ResponseMSg.Content.ReadAs(ResTxt) then
    			ErrorCode := ResTxt
    		else
    			ErrorCode := ResponseMSg.ReasonPhrase;
    		exit(false);
    	end;
    end;
    

    Regards

    Thanks. There is a way to authenticate giving username and password?
  • ftorneroftornero Member Posts: 459
    Hello @weneed,

    Yes, you can use an "Authorization" header for this:
           RequestHeader.Add('Authorization', authorizationValue);
    

    The authorizationValue depends on what the remote web service is expecting.

    Regards.
Sign In or Register to comment.