In 4D 19 R6, we introduced HTTP classes to modernize syntax and enhance functionalities for our HTTP client commands. Building on this foundation, 4D 20 R6 brings a powerful new feature: HTTP agents. These agents empower developers to customize and optimize their connections to HTTP servers by managing connection persistence and reuse for HTTP requests.
HDI Managing HTTP Requests with HTTP Agents
Understanding HTTP Agents
If you have to perform multiple requests to the same HTTP server, you may want to optimize them for both the client and the server. One common way of doing that is to keep a single connection for all requests so you don’t have to negotiate a new connection between requests, especially a secured one. That’s when agents come into play, as agents handle the reuse of connections to optimize your requests.
First of all, it’s important to know that even if you don’t change anything in your code, you will benefit from agents: all your HTTPRequests will use a default agent if you don’t provide one. This agent will manage the persistence of your connections through a simple keep-alive mechanism, so when you send multiple requests to the same server, it will reuse connections if possible instead of creating a new one for each request.
This default agent is the simplest you can imagine, but you may (and should) want to create your own agent. Agents can allow you to fine-tune your connections, giving you control over the keep-alive mechanism, allowing you to choose the maximum number of concurrent connections to a specific server, adding timeouts to avoid keeping connections indefinitely or even configuring your TLS/SSL connections at agent level instead of doing it for each HTTPRequest.
Using an agent for an HTTPRequest is extremely easy; just use this bit of code:
var $requestOptions:={/*here you put the agent options*/}
var $myAgent:=4D.HTTPAgent.new($requestOptions)
var $requestOptions:={}
$requestOptions.agent:=$myAgent
var $myRequest:=4D.HTTPRequest.new("www.4D.com"; $requestOptions)
Tip 1
HTTPAgent is a shared object. Consequently, you can add one to a singleton class to use the same agent for all your requests to the same server.
Tip 2
You can request multiple servers using the same agent. In that case, each server will have its own pool of connections using the same agent options.
With the agents, HTTP classes now cover all the functionalities of the HTTP commands and even more, so it’s a good moment to switch entirely to this new syntax. If you have any questions about HTTP classes or agents specifically, don’t hesitate to post them on the 4D forum.