KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Modify HTTPRequest default callback functions
PRODUCT: 4D | VERSION: 19 R | PLATFORM: Mac & Win
Published On: September 12, 2022

When implementing callbacks for HTTPRequest in v19 R6, functions such as “onHeaders” and “onResponse” are triggered by default in the custom options class. Callbacks like “onResponse”, for instance, can be changed by simply reassigning it, like below:

This.onResponse:=This.myResponse

The above may be executed in the class constructor of the HTTP options class, for example, so that the function “myResponse” will be triggered as the callback instead of “onResponse”. This is useful for chaining different HTTPRequests without needing to create more HTTP options classes. For example:

// myHTTPOptions class
Class constructor()
// Assign onResponse callback
This.onResponse:=This.firstResponse

Function firstResponse($request : 4D.HTTPRequest)
// Reassign onResponse callback
This.onResponse:=This.secondResponse
// Then send another request
$2ndRequest:=4D.HTTPRequest.new($url; This)

Function secondResponse($request : 4D.HTTPRequest)
// more code

In the above code, after the 2nd request is sent, “secondResponse” would be triggered as the “onResponse” callback.