Loading...
Searching...
No Matches
ml::NetworkResponse Struct Reference

The result of an HTTP or WebSocket message. More...

#include <Malena/Engine/Networking/NetworkResponse.h>

Public Member Functions

bool hasHeader (const std::string &key) const
 Return true when a header with key is present.
const std::string & header (const std::string &key, const std::string &fallback="") const
 Return the value of header key, or fallback if absent.
bool ok () const
 Return true when the request succeeded with a 2xx status.

Public Attributes

std::string body
 Response body as a UTF-8 string.
std::string error
 Non-empty when a transport or connection error occurred.
std::unordered_map< std::string, std::string > headers
 Response headers, lower-cased keys.
int statusCode = 0
 HTTP status code (200, 404, 500 …). 0 on transport/connection failure.

Detailed Description

The result of an HTTP or WebSocket message.

Returned to every HttpClient callback and carried by WebSocketClient message events. Check ok() before accessing body — if the transport failed, error describes the reason and statusCode is 0.

client.get("/api/questions", [](ml::NetworkResponse resp) {
if (!resp.ok()) {
std::cerr << "Request failed: " << resp.error << "\n";
return;
}
// resp.statusCode == 200, resp.body contains JSON
});
The result of an HTTP or WebSocket message.
std::string error
Non-empty when a transport or connection error occurred.
bool ok() const
Return true when the request succeeded with a 2xx status.
See also
HttpClient, WebSocketClient

Definition at line 36 of file NetworkResponse.h.

Member Function Documentation

◆ hasHeader()

bool ml::NetworkResponse::hasHeader ( const std::string & key) const
inlinenodiscard

Return true when a header with key is present.

Parameters
keyHeader name (case-insensitive — stored lower-cased).

Definition at line 65 of file NetworkResponse.h.

◆ header()

const std::string & ml::NetworkResponse::header ( const std::string & key,
const std::string & fallback = "" ) const
inlinenodiscard

Return the value of header key, or fallback if absent.

Parameters
keyHeader name (case-insensitive).
fallbackValue returned when the header is not present.

Definition at line 75 of file NetworkResponse.h.

◆ ok()

bool ml::NetworkResponse::ok ( ) const
inlinenodiscard

Return true when the request succeeded with a 2xx status.

A response is considered successful when there is no transport error and the HTTP status is in the [200, 299] range.

Definition at line 56 of file NetworkResponse.h.

Member Data Documentation

◆ body

std::string ml::NetworkResponse::body

Response body as a UTF-8 string.

Definition at line 42 of file NetworkResponse.h.

◆ error

std::string ml::NetworkResponse::error

Non-empty when a transport or connection error occurred.

Definition at line 48 of file NetworkResponse.h.

◆ headers

std::unordered_map<std::string, std::string> ml::NetworkResponse::headers

Response headers, lower-cased keys.

Definition at line 45 of file NetworkResponse.h.

◆ statusCode

int ml::NetworkResponse::statusCode = 0

HTTP status code (200, 404, 500 …). 0 on transport/connection failure.

Definition at line 39 of file NetworkResponse.h.


The documentation for this struct was generated from the following file: