4#ifndef MALENA_HTTPCLIENT_H
5#define MALENA_HTTPCLIENT_H
14#include <unordered_map>
15#include <nlohmann/json.hpp>
115 void post(
const std::string& path,
116 const std::string& body,
118 const std::string& contentType =
"application/json");
123 void put(
const std::string& path,
124 const std::string& body,
126 const std::string& contentType =
"application/json");
132 const std::string& body,
134 const std::string& contentType =
"application/json");
149 void post(
const std::string& path,
150 const nlohmann::json&
json,
153 post(path,
json.dump(), std::move(cb));
159 void put(
const std::string& path,
160 const nlohmann::json&
json,
163 put(path,
json.dump(), std::move(cb));
170 const nlohmann::json&
json,
199 std::string _baseUrl;
200 int _defaultTimeout = 30;
201 std::unordered_map<std::string, std::string> _defaultHeaders;
207 [[nodiscard]] std::string resolveUrl(
const std::string& path)
const;
void del(const std::string &path, ResponseCallback cb)
Send a DELETE request asynchronously.
void post(const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json")
Send a POST request with a body asynchronously.
HttpClient(const std::string &baseUrl="")
Construct an HttpClient with an optional base URL.
void setDefaultHeader(const std::string &key, const std::string &value)
Add a default header sent with every request.
void setBaseUrl(const std::string &url)
Set or replace the base URL (scheme + host + optional port).
NetworkResponse sendSync(const NetworkRequest &request)
Execute request synchronously on the calling thread.
std::function< void(NetworkResponse)> ResponseCallback
void setBearerToken(const std::string &token)
Set the Authorization: Bearer token applied to all requests.
void send(const NetworkRequest &request, ResponseCallback cb)
Send an arbitrary NetworkRequest asynchronously.
void patch(const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json")
Send a PATCH request with a body asynchronously.
void put(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a PUT request with a nlohmann::json body.
void patch(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a PATCH request with a nlohmann::json body.
void get(const std::string &path, ResponseCallback cb)
Send a GET request asynchronously.
void put(const std::string &path, const std::string &body, ResponseCallback cb, const std::string &contentType="application/json")
Send a PUT request with a body asynchronously.
void post(const std::string &path, const nlohmann::json &json, ResponseCallback cb)
Send a POST request with a nlohmann::json body.
void setDefaultTimeout(int seconds)
Set the default timeout in seconds (default 30).
Fluent builder for an outgoing HTTP request.
nlohmann::json json
Alias for nlohmann::json.
The result of an HTTP or WebSocket message.