Loading...
Searching...
No Matches
ml::NetworkRequest Class Reference

Fluent builder for an outgoing HTTP request. More...

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

Public Member Functions

 NetworkRequest (const std::string &url)
 Construct a request for url.
NetworkRequestbasicAuth (const std::string &user, const std::string &pass)
 Set HTTP Basic Authentication credentials.
NetworkRequestbearerToken (const std::string &token)
 Set the Authorization header to "Bearer <token>".
NetworkRequestbody (const nlohmann::json &json)
 Set the request body from a nlohmann::json value.
NetworkRequestbody (const std::string &data, const std::string &contentType="application/json")
 Set the request body.
const std::string & getBody () const
const std::string & getContentType () const
const std::unordered_map< std::string, std::string > & getHeaders () const
const std::string & getMethod () const
int getTimeout () const
const std::string & getUrl () const
NetworkRequestheader (const std::string &key, const std::string &value)
 Add or replace a request header.
NetworkRequestmethod (const std::string &m)
 Override the HTTP method.
NetworkRequesttimeout (int seconds)
 Set the request timeout in seconds. Default is 30 s.

Detailed Description

Fluent builder for an outgoing HTTP request.

Construct a NetworkRequest with a URL, then chain method calls to configure headers, body, and other options before passing it to HttpClient::send().

auto req = ml::NetworkRequest("https://api.example.com/submit")
.method("POST")
.body(R"({"answer":"42"})")
.bearerToken(myToken)
.timeout(10);
client.send(req, [](ml::NetworkResponse resp){ ... });
Fluent builder for an outgoing HTTP request.
NetworkRequest & method(const std::string &m)
Override the HTTP method.
NetworkRequest & timeout(int seconds)
Set the request timeout in seconds. Default is 30 s.
NetworkRequest & body(const std::string &data, const std::string &contentType="application/json")
Set the request body.
NetworkRequest & bearerToken(const std::string &token)
Set the Authorization header to "Bearer <token>".
The result of an HTTP or WebSocket message.
See also
HttpClient, NetworkResponse

Definition at line 36 of file NetworkRequest.h.

Constructor & Destructor Documentation

◆ NetworkRequest()

ml::NetworkRequest::NetworkRequest ( const std::string & url)
explicit

Construct a request for url.

The method defaults to GET. Add headers and a body before sending.

Parameters
urlFully-qualified URL including scheme (http:// or https://).

Member Function Documentation

◆ basicAuth()

NetworkRequest & ml::NetworkRequest::basicAuth ( const std::string & user,
const std::string & pass )

Set HTTP Basic Authentication credentials.

Encodes user and pass as Base64 and sets the Authorization header.

◆ bearerToken()

NetworkRequest & ml::NetworkRequest::bearerToken ( const std::string & token)

Set the Authorization header to "Bearer <token>".

Parameters
tokenBearer token string.

◆ body() [1/2]

NetworkRequest & ml::NetworkRequest::body ( const nlohmann::json & json)
inline

Set the request body from a nlohmann::json value.

Serialises json to a string and sets Content-Type to application/json automatically.

auto req = ml::NetworkRequest("https://api.example.com/submit")
.method("POST")
.body({{"answer", "42"}, {"questionId", 7}})
.bearerToken(token);

Definition at line 108 of file NetworkRequest.h.

◆ body() [2/2]

NetworkRequest & ml::NetworkRequest::body ( const std::string & data,
const std::string & contentType = "application/json" )

Set the request body.

Parameters
dataRaw body string.
contentTypeValue for the Content-Type header. Defaults to "application/json".

◆ getBody()

const std::string & ml::NetworkRequest::getBody ( ) const
inlinenodiscard

Definition at line 125 of file NetworkRequest.h.

◆ getContentType()

const std::string & ml::NetworkRequest::getContentType ( ) const
inlinenodiscard

Definition at line 126 of file NetworkRequest.h.

◆ getHeaders()

const std::unordered_map< std::string, std::string > & ml::NetworkRequest::getHeaders ( ) const
inlinenodiscard

Definition at line 130 of file NetworkRequest.h.

◆ getMethod()

const std::string & ml::NetworkRequest::getMethod ( ) const
inlinenodiscard

Definition at line 124 of file NetworkRequest.h.

◆ getTimeout()

int ml::NetworkRequest::getTimeout ( ) const
inlinenodiscard

Definition at line 127 of file NetworkRequest.h.

◆ getUrl()

const std::string & ml::NetworkRequest::getUrl ( ) const
inlinenodiscard

Definition at line 123 of file NetworkRequest.h.

◆ header()

NetworkRequest & ml::NetworkRequest::header ( const std::string & key,
const std::string & value )

Add or replace a request header.

Parameters
keyHeader name (e.g. "X-Request-ID").
valueHeader value.

◆ method()

NetworkRequest & ml::NetworkRequest::method ( const std::string & m)

Override the HTTP method.

Defaults to "GET". Common values: "POST", "PUT", "PATCH", "DELETE".

◆ timeout()

NetworkRequest & ml::NetworkRequest::timeout ( int seconds)

Set the request timeout in seconds. Default is 30 s.

Parameters
secondsMaximum time to wait for a complete response.

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