Skip to content

class SF::Http
inherits Reference #

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 not supported.

The HTTP client is split into 3 classes:

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
http = SF::Http.new("http://www.sfml-dev.org")

# Prepare a request to get the 'features.php' page
request = SF::Http::Request.new("features.php")

# Send the request
response = http.send_request request

# Check the status code and display the result
status = response.status
if status.ok?
  puts response.body
else
  puts "Error #{response.status}"
end

Included modules

SF::NonCopyable

Constructors#

.new(host : String, port : Int = 0)#

Construct the HTTP client with the target host

This is equivalent to calling host=(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.

  • host - Web server to connect to
  • port - Port to use for connection
View source

.new#

Default constructor

View source

Methods#

#finalize#

View source

#send_request(request : Http::Request, timeout : Time = Time::Zero) : Http::Response#

Send a HTTP request and return the server's response.

You must have a valid host before sending a request (see host=). 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).

  • request - Request to send
  • timeout - Maximum time to wait

Returns: Server's response

View source

#set_host(host : String, port : Int = 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). You should leave it like this unless you really need a port other than the standard one, or use an unknown protocol.

  • host - Web server to connect to
  • port - Port to use for connection
View source