4D Blog

Home Product Energize your Web applications with the OutgoingMessage Class

Energize your Web applications with the OutgoingMessage Class

November 14, 2024

Product

In today’s digital world, a smooth and intuitive user experience is key to any web application’s success. A critical part of this experience is providing users with easy access to various resources—whether documents, images, or other data types. To make this possible, your server must manage and deliver different content formats efficiently.

With 4D’s 20 R7 new REST server feature, managing diverse content delivery is now easier than ever. Previously, the REST server could only return scalar, entity, or entity selection data. Now, it can deliver full web content directly—content ready for a browser to handle.

Downloading files or receiving binary data from the REST server is now straightforward, thanks to the new OutgoingMessage class. With it, you can easily customize responses to suit your needs.

Read on to explore how the OutgoingMessage class can enhance your application!

HDI_OutGoingMessageClass

calling an exposed function on the REST Server

Quick reminder: ORDA data model functions and singleton functions are callable as REST APIs. To refresh your memory, check out this blog post and this one.

Until now, the REST server could return various result types—scalars, entities, or entity selections—wrapped within an object using the result property.

example

This is the getSomeInfo() function on the Datastore class.

exposed Function getSomeInfo() : Text
	return "This is the info"

And here is the result got when it is called with the /rest/$catalog/getSomeInfo request:

{
    "result": "This is the info"
}

4D Web commands can’t be used in such functions, that’s why the new OutgoingMessage class comes with a great benefit.

The new OutgoingMessage class

The ORDA data model and singletons functions can now return an object instance of this new OutgoingMessage class.

Such objects are handled directly by any browser as web content.

Thus, you can propose to your end users features such as downloading files and pictures and receiving any content type via a browser.

This is the JSON representation of such an object instance of the OutgoingMessage class.

example

In this example, the getFile() function is implemented in the Datastore class. The purpose is to return the testFile.pdf file as a response to the request.

An object instance of the OutgoingMessage class is created.

The body contains the binary content of the testFile.pdf file and a header indicating that the content type is set.

exposed onHTTPGet Function getFile() : 4D.OutgoingMessage
	
var $result:=4D.OutgoingMessage.new()
var $file:=File("/RESOURCES/testFile.pdf")
	
$result.setBody($file.getContent())  // This is binary content
$result.setHeader("Content-Type"; "application/pdf")
return $result

The OutgoingMessage class contains three properties to fill according to your needs:

  • header: Set any HTTP header that the client is bound to handle (e.g., content-type to indicate the type of the body content)
  • body: set the content you want to send as a response to the request. It can be a String or a Blob (e.g., file, document, or image as binary content).
  • status: Set any HTTP status code according to the result of the proceeded request. This provides the client with information on how the request went (e.g., status 403 if the request is refused). The default value is 200.

 

N.B. The body and status properties can be affected with the := operator or with the corresponding setBody() / setStatus() function.

ease the call of functions on the rest server

the new onHTTPGet keyword for functions

So far, the function calls (of ORDA data model classes and singletons) through the REST server had to be done with a POST verb. This was done for security reasons to avoid running sensitive code through an action as simple as clicking on a link.

Because the POST does not always fit the user experience you want to offer, those functions can also be called with a GET verb, i.e., callable by entering a URL in a browser.

Use the new onHTTPGet keyword for this. If this keyword is applied to a function, this function is also callable with a GET verb!

example

exposed onHTTPGet Function getSomeInfo() : 4D.OutgoingMessage

N.B. As this type of call is an easily offered action, the developer must ensure no sensitive action is done in such functions.

passing parameters

Parameters can be passed to the function with the $params parameter (must be enclosed in a collection).

example

In this example, the getThumbnail() function of the Products dataclass is called (to get a thumbnail photo of a given product).

It receives the product name and the required width + length.

IP:port/rest/Products/getThumbnail?$params='["Yellow Pack",200,200]'

Check the documentation for more information about sending parameters when calling a function.

A complete example

Here is the use case of this example: a link is proposed to an end user to download the user manual of a selected product with several formats available.

The request behind the link is: IP:port/rest/Products/getUserManual?$params='[1, “pdf”]’

The selected product id (1) is passed as a parameter and the required format (pdf).

The getUserManual() function of the Products dataclass is called. It receives the product id and the format as a parameter.

The corresponding document is retrieved. Its binary content is put as a body in the response with the corresponding content type.

exposed onHTTPGet Function getUserManual($productId : Integer; $type : Text) : 4D.OutgoingMessage
	
var $file : 4D.File
var $response:=4D.OutgoingMessage.new()
var $doc:="/RESOURCES/User manuals/product_"+String($productId)

Case of 
	: ($type="pdf")
		$file:=File($doc+".pdf")
                $response.setBody($file.getContent()) // This is binary content 
		$response.setHeader("Content-Type"; "application/pdf")
			
	: ($type="jpeg")
		$file:=File($doc+".jpeg")
                $response.setBody($file.getContent()) // This is binary content 
		$response.setHeader("Content-Type"; "image/jpeg")
End case 
	
return $response

 

Here is the scenario played in a browser:

blank

 

Try It Yourself

Download the HDI to explore this new feature, and check out the documentation for more details about using the OutgoingMessage class in your web applications.

Discuss

Tags HTTP, ORDA Data model classes, Programming, Request, response, REST server, Singleton, v20R7, Web

Latest related posts

  • July 15, 2025

    Smarter Copy-Paste to Protect Your Data

  • July 15, 2025

    ORDA – Get started with the entity constructor

  • July 15, 2025

    Building Forms on the fly with New Data Source Commands

Avatar
Marie-Sophie Landrieu-Yvert
- Product Owner - Marie-Sophie Landrieu-Yvert joined the 4D Product team as a Product Owner in 2017. In this role, she is responsible for writing user stories and translating them into functional specifications. She also ensures that the delivered feature implementation meets the customer's needs. Marie-Sophie graduated from the engineering school ESIGELEC and began her career as an engineer at IBM in 1995. She took part in various projects (maintenance and development projects) and worked as a COBOL developer. She then moved on to work as a UML designer and Java developer. More recently, her main responsibilities included analyzing and writing functional requirements, and coordinating business and development teams.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 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-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Listbox Logs Mail Microsoft 365 Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 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