In our constant effort to improve 4D syntax and functionalities, we have decided to bring you new commands to perform HTTP requests in 4D: the HTTP classes. They are available right in 4D v19 R6 and will feature many improvements over the coming versions. Let me show you how to use them right away.
The basic syntax to send an HTTP request is very simple:
$request:=4D.HTTPRequest.new($url).wait() If ($request.response#Null) // The code to handle the request response. Else // The code to handle errors. End if
The call to 4D.HTTPRequest.new both instantiates the request and sends it. The result of the request will be retrieved inside the response attribute of the HTTPRequest.
At that stage, you’re all wondering what this call to wait is doing there. The new HTTP classes are meant to be used asynchronously. You don’t need to create a specific process to host your request; you can now send a request in the middle of a method without blocking it. But if you want to keep the old synchronous behavior, you can make a call to wait to wait for the request to complete.
Now, let’s see how to make a genuinely asynchronous request:
$request:=4D.HTTPRequest.new($url) // The code that will be executed while the request is not complete. If ($request.terminated) // Is the request complete? If ($request.response#Null) // The code to handle the request response. Else // The code to handle errors. End if End if
Here it is, simple. You can now execute your code while your request is being sent and, as such, benefit from optimal performances.
asynchronous request with callbacks
The last example will be slightly more complicated. The new syntax allows you to handle requests through callbacks. Typically, you’ll write a new class to handle your HTTPRequest:
Class constructor() Function onResponse($request : 4D.HTTPRequest) // The code to handle the request response. Function onError($request : 4D.HTTPRequest) // The code to handle errors.
And you’ll then use this class to automatically handle your requests:
$callbacks:=cs.myClassToHandleHTTPRequests.new() $request:=4D.HTTPRequest.new($url; $callbacks)
No need to handle the request in your method; the request result will be handled thanks to the onResponse and onError callbacks as soon as a response is available. You have to be wary of only one thing: The callbacks are called in the same process as your method. You will miss the request result if you kill the process before they are called. Be sure at the end of your method to check that all your requests have been completed.
For the last example, let’s look at an actual situation. We will request a Trivia REST API and submit a random question to the user. Here’s the class to handle the request:
Class constructor() Function onResponse($request : 4D.HTTPRequest) If ($request.response.status=200) ALERT("Question: "+String($request.response.body.question)) ALERT("Answer: "+String($request.response.body.answer)) Else This.onError($request) End if Function onError($request : 4D.HTTPRequest) ALERT("Error when loading the question")
And then you can send your request:
$callbacks:=cs.TriviaRequestClass.new() $request:=4D.HTTPRequest.new("http://jservice.io/api/random"; $callbacks)
Have fun answering random trivia questions!
How do I
Beautiful weather in France today
To illustrate this feature, here’s a demo example interrogating a weather forecast REST service. You will see all these different requests in action to provide you with the best weather forecast as long as you live in (or plan to travel to) France.
This is the first blog post about the new HTTP classes, be sure this is one of a long series as new HTTP functionalities will be released soon!
As usual, if you have any comments, feel free to share them with us on the official 4D forum.