Consume external soap Webservice from SaaS Business Central

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

Best Answer

  • weneedweneed Member Posts: 82
    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: 524
    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: 82
    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: 524
    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.
  • weneedweneed Member Posts: 82
    Hi @ftornero,

    who if I want to use windows authentication?
    How I can insert Username and password?
  • ftorneroftornero Member Posts: 524
    Hello @weneed,

    I guess that this post is related to the new one that you opened.

    Regards.
  • weneedweneed Member Posts: 82
    Hellp @ftornero. Many thanks.
    I've solved windows authentication but bad request is response. so I opened another question
Sign In or Register to comment.