Loading...
Searching...
No Matches
NetworkResponse.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_NETWORKRESPONSE_H
5#define MALENA_NETWORKRESPONSE_H
6
7#pragma once
8
10#include <string>
11#include <unordered_map>
12
13namespace ml
14{
37 {
39 int statusCode = 0;
40
42 std::string body;
43
45 std::unordered_map<std::string, std::string> headers;
46
48 std::string error;
49
56 [[nodiscard]] bool ok() const
57 {
58 return error.empty() && statusCode >= 200 && statusCode < 300;
59 }
60
65 [[nodiscard]] bool hasHeader(const std::string& key) const
66 {
67 return headers.count(key) > 0;
68 }
69
75 [[nodiscard]] const std::string& header(const std::string& key,
76 const std::string& fallback = "") const
77 {
78 auto it = headers.find(key);
79 return it != headers.end() ? it->second : fallback;
80 }
81 };
82
83} // namespace ml
84
85#endif // MALENA_NETWORKRESPONSE_H
#define MALENA_API
Definition Component.h:22
The result of an HTTP or WebSocket message.
std::string error
Non-empty when a transport or connection error occurred.
int statusCode
HTTP status code (200, 404, 500 …). 0 on transport/connection failure.
bool hasHeader(const std::string &key) const
Return true when a header with key is present.
std::string body
Response body as a UTF-8 string.
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.
std::unordered_map< std::string, std::string > headers
Response headers, lower-cased keys.