Responses
Introduction
The response of a service is the result sent to the client after processing a request. It can have different formats depending on the need, such as JSON for structured data, plain text for simple messages, HTML for web content, binary files for downloads, or even streaming responses for progressively sent data. Its content and status code (like 200 for success or 400 for errors) indicate the result of the operation performed.
In its actions, Cafeto uses the standard responses from Starlette
, which include: JSON
, HTML
, Plain Text
, Binary Files
, Streaming
, or None
(no content). Additionally, Cafeto offers two extra responses to handle DTOs: ModelResponse
and ModelWSResponse
(specific for websockets). This approach is called the Classic Method
.
Complementing the responses of the Classic Method
, Cafeto introduces an additional system called the Unified Method
.
The main difference is that in the Classic Method
, the developer must manually choose the type of response according to the data to be returned. In contrast, the Unified Method
always uses a single response that can automatically detect the type of data to be returned, simplifying the process.
In the Classic Method
, the default status code (statusCode
) is always 200
, and it must be adjusted manually according to the needs. On the other hand, in the Unified Method
, each type of response is automatically associated with a specific status code.
JSON Response
It is important to remember that the response type of the actions is not strictly mandatory and is only used for documentation with OpenApi, but it is suggested to use it to have a clearer and easier to maintain project.
DTO Response
List of DTOs Response
No Content Response
File or Binary Response
Plain Text Response
HTML Response
Streaming Response
Conclusion
Both systems are quite good, each with its pros and cons. It is up to each developer to use one or the other. It is recommended to choose one from the beginning to avoid confusion and not to combine the two systems, thus having greater order and control over the system.