- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2024
11:33 PM
12-04-2024
11:33 PM
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;