cancel
Showing results for 
Search instead for 
Did you mean: 

Get data from and send data to an external REST service

Get data from and send data to an external REST service

The following example combines all the different HTTP classes and functions to retrieve data from an external service. The external service returns a JSON document with user information. You can use the jsonplaceholder.typicode.com website to test REST services. Additionally, you can request, add, and edit data from that website because it is a free space for you to practice.

JSONCopy
 
procedure GetUserInformation(UserNumber: Integer)
var
    Client: HttpClient;
    ResponseMessage: HttpResponseMessage;
    ResponseString: Text;
begin
    if not Client.Get(StrSubstNo('https://jsonplaceholder.typicode.com/users/%1', 
                      UserNumber), ResponseMessage) then
        Error('The call to the web service failed.');

    if not ResponseMessage.IsSuccessStatusCode() then
        Error('The web service returned an error message:\\' +
                'Status code: ' + Format(ResponseMessage.HttpStatusCode()) +
                'Description: ' + ResponseMessage.ReasonPhrase());

    ResponseMessage.Content().ReadAs(ResponseString);
end;

ai artificial intelligence training courses malaysia