Class HTTP#
Defined in File HTTP.h
Nested Relationships#
Nested Types#
Class Documentation#
-
class HTTP#
A HTTP client. Prioritizes IPv6 over IPv4.
Public Functions
-
HTTP() noexcept#
Constructor.
-
explicit HTTP(const std::string &host, u16 port = 0)#
Construct the HTTP client with the target host.
This is equivalent to calling SetHost(host, port). The port has a default value of 0, which means that the HTTP client will use the right port according to the protocol used (80 for HTTP). You should leave it like this unless you rally need a part other than the standard one, or use an unknown protocol.
- Parameters:
host – Web server to connect to.
port – Port to use for connection.
-
~HTTP() = default#
Destructor.
-
void SetHost(const std::string &host, u16 port = 0)#
Set target host.
This function just stores the host address and port, it doesn’t actually connect to it until you send a request. The port has a default value of 0, which means that the HTTP client will use the right port according to the protocol used (80 for HTTP). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.
- Parameters:
host – Web server to connect to.
port – Port to use for connection.
-
Response SendRequest(const Request &request, Utils::TimeStep timeout = Utils::TimeStep(0.0f))#
Send a HTTP request and return the server’s response.
You must have a valid host before sending a request (see SetHost). Any missing mandatory header field in the request will be added with an appropriate value.
A value of Utils::TimeStep(0.0f) means that the client will use the system default timeout (which is usually pretty long).
Warning
This function waits for the server’s response and may not return instantly; use a thread if you don’t want to block your application, or use a timeout to limit the time to wait.
- Parameters:
request – Request to send.
timeout – Maximum time to wait.
- Returns:
Server response.
-
class Request#
Define a HTTP request.
Public Types
Public Functions
-
explicit Request(std::string uri = "/", Method method = Method::GET, std::string body = "")#
Constructor.
This constructor creates a GET request, with the root URI (“/”) and an empty body.
- Parameters:
uri – Target URI.
method – Method to use for the request.
body – Content of the request’s body.
-
~Request() = default#
Destructor.
-
void SetField(const std::string &field, const std::string &value)#
Set the value of a field.
The field is created if it doesn’t exist. The name of the field is case-insensitive. By default, a request doesn’t contain any field (but the mandatory fields are added later by the HTTP client when sending the request).
- Parameters:
field – Name of the field to set.
value – Value of the field.
-
constexpr void SetMethod(Method method) noexcept#
Set the request method.
See the Method enumeration for a complete list of all the available methods. The method is HTTP::Request::GET by default.
- Parameters:
method – Method to use for the request.
-
constexpr void SetURI(std::string uri)#
Set the requested URI.
The URI is the resource (usually a web page or a file) that you want to get or post. The URI is “/” (the root page) by default.
- Parameters:
uri – URI to request, relative to the host.
-
constexpr void SetHTTPVersion(u32 major, u32 minor) noexcept#
Set the HTTP version for the request.
The HTTP version is 1.0 by default.
-
constexpr void SetBody(std::string body)#
Set the body of the request.
The body of a request is optional and only makes sense for POST requests. It is ignored for all other methods. The body is empty by default.
- Parameters:
body – Content of the body.
-
explicit Request(std::string uri = "/", Method method = Method::GET, std::string body = "")#
-
class Response#
Define a HTTP response.
Public Types
-
enum class Status#
Enumerate all the valid status codes for a response.
Values:
-
enumerator OK#
-
enumerator Created#
-
enumerator Accepted#
-
enumerator NoContent#
-
enumerator ResetContent#
-
enumerator PartialContent#
-
enumerator MultipleChoices#
-
enumerator MovedPermanently#
-
enumerator MovedTemporarily#
-
enumerator NotModified#
-
enumerator BadRequest#
-
enumerator Unauthorized#
-
enumerator Forbidden#
-
enumerator NotFound#
-
enumerator RangeNotSatisfiable#
-
enumerator InternalServerError#
-
enumerator NotImplemented#
-
enumerator BadGateway#
-
enumerator ServiceNotAvailable#
-
enumerator GatewayTimeout#
-
enumerator VersionNotSupported#
-
enumerator InvalidResponse#
-
enumerator ConnectionFailed#
-
enumerator OK#
Public Functions
-
Response() noexcept#
Constructor.
-
~Response() = default#
Destructor.
-
std::string GetField(const std::string &field) const#
Get the value of a field.
If the field field is not found in the response header, the empty string is returned. This function uses case-insensitive comparisons.
- Parameters:
field – Name of the field to get.
- Returns:
Value of the field, or empty string if not found.
-
constexpr Status GetStatus() const noexcept#
Get the response status code.
The status code should be the first thing to be checked after receiving a response, it defines whether it is a success, a failure or anything else (see the Status enumeration).
- Returns:
Status code of the response.
-
constexpr u32 GetMajorHTTPVersion() const noexcept#
Get the major HTTP version number of the response.
- Returns:
Major HTTP version number.
-
constexpr u32 GetMinorHTTPVersion() const noexcept#
Get the minor HTTP version number of the response.
- Returns:
Minor HTTP version number.
-
constexpr std::string GetBody() const noexcept#
Get the body of the response.
The body of a response may contain:
the requested page (for GET requests)
a response from the server (for POST requests)
nothing (for HEAD requests)
an error message (in case of an error)
- Returns:
The response body.
-
enum class Status#
-
HTTP() noexcept#