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.
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;
The ResponseString variable will contain the JSON document as a string. You can use the built-in JSON classes to parse the string and work with the content. You can also use the HttpClient class to upload data to a REST service. The subsequent code example sends data and an image to an external service. This service can, for example, perform image processing. In this example, the first picture from a certain item is retrieved. The Picture field in Items is a MediaSet field; therefore, it can contain multiple images. Those images are stored in the Tenant Media table.
java programming training courses malaysia