Options

Call web services From Business central to another Business central

ivancito86ivancito86 Member Posts: 18
Hello!!,

I have to do a call from AL with the way POST, but I don't get works. Somebody knows what is my problem???

The function WSClientes has one parameter only and its returned a bool variable.



payload := '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/&quot; xmlns:tem="http://tempuri.org/"><soap:Header/><soap:Body><tem:WSClientes>';
payload += STRSUBSTNO('<tem:vCodCliente>%1</tem:vCodCliente>', '222');
payload += '</tem:WSClientes></soap:Body></soap:Envelope>';
codeunit50001.CallService('https://api.businesscentral.dynamics.com/v1.0/b24fc7cc-4266-4c50-841a-bcb2f9c02887/Sandbox/WS/SOTER/Codeunit/WSSEATRA', 2, payload, '', '');



And the procedure to call is the next:

procedure CallService(RequestUrl: Text; RequestType: Integer; payload: Text; Username: Text; Password: Text): Text
var
Client: HttpClient;
RequestHeaders: HttpHeaders;
RequestContent: HttpContent;
ResponseMessage: HttpResponseMessage;
RequestMessage: HttpRequestMessage;
ResponseText: Text;
contentHeaders: HttpHeaders;
begin
//RequestType
//0; Get
//1; patch
//2; post
//3; delete

RequestHeaders := Client.DefaultRequestHeaders();
if Username <> '' then
RequestHeaders.Add('Authorization', CreateBasicAuthHeader(Username, Password));

case RequestType of
0:
Client.Get(RequestURL, ResponseMessage);
1:
begin
RequestContent.WriteFrom(payload);
RequestContent.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'application/json-patch+json');
RequestMessage.Content := RequestContent;
RequestMessage.SetRequestUri(RequestURL);
RequestMessage.Method := 'PATCH';
client.Send(RequestMessage, ResponseMessage);
end;
2:
begin
RequestContent.WriteFrom(payload);
RequestContent.GetHeaders(contentHeaders);
contentHeaders.Clear();
contentHeaders.Add('Content-Type', 'text / xml');
Client.Post(RequestURL, RequestContent, ResponseMessage);
end;
3:
Client.Delete(RequestURL, ResponseMessage);
end;
ResponseMessage.Content().ReadAs(ResponseText);
exit(ResponseText);
end;


Best regards.
Sign In or Register to comment.