View on GitHub

DSFML

dsfml.network.http

A module containing the Http class.

class Http;
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.

this();
Default constructor

this(string host, ushort 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, 443 for HTTPS). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.





Params:
string host Web server to connect to.
ushort port Port to use for connection.

void setHost(string host, ushort port = 0);
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. 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, 443 for HTTPS). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.





Params:
string host Web server to connect to.
ushort port Port to use for connection.

Response sendRequest(Request request, Time timeout = Time.Zero);
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 defaut timeout (which is usually pretty long).





Params:
Request request Request to send.
Time timeout Maximum time to wait.

class Request;
Define a HTTP request.

enum Method: int;
Enumerate the available HTTP methods for a request.

Get
Request in get mode, standard method to retrieve a page.

Post
Request in post mode, usually to send data to a page.

Head
Request a page's header only.

this(string uri = "/", Method method = Method.Get, string requestBody = "");
This constructor creates a GET request, with the root URI ("/") and an empty body.

Params:
string uri Target URI.
Method method Method to use for the request.
body Content of the request's body.

void setBody(string requestBody);
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.





Params:
body Content of the body.

void setField(string feild, 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).





Params:
field Name of the field to set.
string value Value of the field.

void setHttpVersion(uint major, uint minor);
Set the HTTP version for the request.

The HTTP version is 1.0 by default.





Parameters

major = Major HTTP version number.

minor = Minor HTTP version number.

void setMethod(Method method);
Set the request method.

See the Method enumeration for a complete list of all the availale methods. The method is Http::Request::Get by default.





Params

method = Method to use for the request.

void setUri(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.





Params

uri = URI to request, relative to the host.

class Response;
Define a HTTP response.

enum Status: int;
Enumerate all the valid status codes for a response.

string getBody();
Get the body of the response.

Returns:
The response body.

string getField(string field);
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.





Params:
string field Name of the field to get.

Returns:
Value of the field, or empty string if not found.

uint getMajorHttpVersion();
Get the major HTTP version number of the response.

Returns:
Major HTTP version number.

uint getMinorHttpVersion();
Get the minor HTTP version number of the response.

Returns:
Minor HTTP version number.

Status getStatus();
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.