SFML 3.1.0
Loading...
Searching...
No Matches
Classes | Public Member Functions | List of all members
sf::Http Class Reference
Network module

A HTTP client. More...

`#include <SFML/Network/Http.hpp>`

## Classes
class | Request
| HTTP request. More...
class | Response
| HTTP response. More...

## Public Member Functions
| Http ()=default
| Default constructor.
| Http (const std::string &host, unsigned short port=0, std::optional< IpAddress::Type > addressType=std::nullopt)
| Construct the HTTP client with the target host.
| Http (const Http &)=delete
| Deleted copy constructor.
Http & | operator= (const Http &)=delete
| Deleted copy assignment.
bool | setHost (const std::string &host, unsigned short port=0, std::optional< IpAddress::Type > addressType=std::nullopt)
| Set the target host.
Response | sendRequest (const Request &request, Time timeout=Time::Zero, bool verifyServer=true) const
| Send a HTTP request and return the server's response.

## Detailed Description

A HTTP client.

sf::Http is a very simple HTTP client that allows you to communicate with a web server.

You can retrieve web pages, send data to an interactive resource, download a remote file, etc. The HTTPS protocol is supported using TLS connections only.

The HTTP client is split into 3 classes:

- sf::Http::Request
- sf::Http::Response
- sf::Http

sf::Http::Request builds the request that will be sent to the server. A request is made of:

- a method (what you want to do)
- a target URI (usually the name of the web page or file)
- one or more header fields (options that you can pass to the server)
- an optional body (for POST requests)

sf::Http::Response parse the response from the web server and provides getters to read them. The response contains:

- a status code
- header fields (that may be answers to the ones that you requested)
- a body, which contains the contents of the requested resource

sf::Http provides a simple function, SendRequest, to send a sf::Http::Request and return the corresponding sf::Http::Response from the server.

Usage example:

// Create a new HTTP client
sf::Http http;
// We'll work on https://www.sfml-dev.org
http.setHost("https://www.sfml-dev.org");
// Prepare a request to get the '/learn/' page
sf::Http::Request request("/learn/");
// Send the request
sf::Http::Response response = http.sendRequest(request);
// Check the status code and display the result
sf::Http::Response::Status status = response.getStatus();
if (status == sf::Http::Response::Status::Ok)
{
std::cout << response.getBody() << std::endl;
}
else
{
std::cout << "Error " << status << std::endl;
}
sf::Http::Request
HTTP request.
Definition Http.hpp:58
sf::Http::Response
HTTP response.
Definition Http.hpp:195
sf::Http::Response::getStatus
Status getStatus() const
Get the response status code.
sf::Http::Response::Status
Status
Enumerate all the valid status codes for a response.
Definition Http.hpp:202
sf::Http::Response::Status::Ok
@ Ok
Most common code returned when operation was successful.
Definition Http.hpp:204
sf::Http::Response::getBody
const std::string & getBody() const
Get the body of the response.
sf::Http
A HTTP client.
Definition Http.hpp:51
sf::Http::setHost
bool setHost(const std::string &host, unsigned short port=0, std::optional< IpAddress::Type > addressType=std::nullopt)
Set the target host.
sf::Http::sendRequest
Response sendRequest(const Request &request, Time timeout=Time::Zero, bool verifyServer=true) const
Send a HTTP request and return the server's response.

Definition at line 50 of file Http.hpp.

## Constructor & Destructor Documentation

## ◆ Http() [1/3]

sf::Http::Http | ( |  | ) |
| default

Default constructor.

## ◆ Http() [2/3]

sf::Http::Http | ( | const std::string & | host,
|  | unsigned short | port = 0,
|  | std::optional< IpAddress::Type > | addressType = std::nullopt )

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 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 the connection
  addressType | Address type to use for the connection, std::nullopt to specify no preference

## ◆ Http() [3/3]

sf::Http::Http | ( | const Http & |  | ) |
| delete

Deleted copy constructor.

## Member Function Documentation

## ◆ operator=()

Http & sf::Http::operator= | ( | const Http & |  | ) |
| delete

Deleted copy assignment.

## ◆ sendRequest()

Response sf::Http::sendRequest | ( | const Request & | request,
|  | Time | timeout = Time::Zero,
|  | bool | verifyServer = true ) const
| nodiscard

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. 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. A value of Time::Zero means that the client will use the system default timeout (which is usually pretty long).

Parameters

  request | Request to send
  timeout | Maximum time to wait
  verifyServer | Verify the server if using HTTPS

Returns
  Server's response

## ◆ setHost()

bool sf::Http::setHost | ( | const std::string & | host,
|  | unsigned short | port = 0,
|  | std::optional< IpAddress::Type > | addressType = std::nullopt )

Set the target host.

This function just stores the host address and port, it doesn't actually connect to it until you send a request. It does however try to resolve the address. 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 the connection
  addressType | Address type to use for the connection, std::nullopt to specify no preference

Returns
  true if the host has been resolved and is valid, false otherwise

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

- Http.hpp