4D Blog

Home Product HTTP classes: A New Way to Perform Asynchronous HTTP Requests

HTTP classes: A New Way to Perform Asynchronous HTTP Requests

July 19, 2022

Product

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.

HDI New HTTP Classes In Action 

Simple request

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.

Asynchronous Request

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.

Example

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[0].question))
	ALERT("Answer: "+String($request.response.body[0].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.

Discuss

Tags HTTP, Programming, v19 R6, v20

Latest related posts

  • February 3, 2026

    4D Write Pro – Adding a margin automatically when bullets are set using standard actions

  • January 22, 2026

    Transform Static Documents into Actionable Knowledge with AIKit

  • January 22, 2026

    Deploy Fluent UI effortlessly in your 4D applications

Nicolas Brachfogel
Nicolas Brachfogel
Product Owner & Senior Developer - Nicolas Brachfogel joined 4D in 2017 as senior developer (4D Server and networking) and as Product Owner to manage the 4D version on Apple Silicon. He is tasked with the redaction of user stories and functional specifications, as well as the verification that the new features are in line with customers' needs. With a degree from Institut Supérieur d'Informatique Appliquée Paris (INSIA), Nicolas started his career as a software developer in 2001. After many years of development in Java and C++, he specialized himself in client-server development in the video game industry. As a developer/software architect, he worked on the server architectures of many games (Dofus Arena, Drakerz, Trivial Pursuit Go !).
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 21 R2 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor
Subscribe to 4D Newsletter

© 2026 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy