HTTPClient

Inherits: Reference < Object

Category: Core

Brief Description

Hyper-text transfer protocol client.

Member Functions

void close ( )
Error connect ( String host, int port, bool use_ssl=false, bool verify_host=true )
StreamPeer get_connection ( ) const
int get_response_body_length ( ) const
int get_response_code ( ) const
StringArray get_response_headers ( )
Dictionary get_response_headers_as_dictionary ( )
int get_status ( ) const
bool has_response ( ) const
bool is_blocking_mode_enabled ( ) const
bool is_response_chunked ( ) const
Error poll ( )
String query_string_from_dict ( Dictionary fields )
RawArray read_response_body_chunk ( )
int request ( int method, String url, StringArray headers, String body=”” )
int request_raw ( int method, String url, StringArray headers, RawArray body )
int send_body_data ( RawArray body )
int send_body_text ( String body )
void set_blocking_mode ( bool enabled )
void set_connection ( StreamPeer connection )
void set_read_chunk_size ( int bytes )

Numeric Constants

  • METHOD_GET = 0
  • METHOD_HEAD = 1
  • METHOD_POST = 2
  • METHOD_PUT = 3
  • METHOD_DELETE = 4
  • METHOD_OPTIONS = 5
  • METHOD_TRACE = 6
  • METHOD_CONNECT = 7
  • METHOD_MAX = 8
  • STATUS_DISCONNECTED = 0
  • STATUS_RESOLVING = 1
  • STATUS_CANT_RESOLVE = 2
  • STATUS_CONNECTING = 3
  • STATUS_CANT_CONNECT = 4
  • STATUS_CONNECTED = 5
  • STATUS_REQUESTING = 6
  • STATUS_BODY = 7
  • STATUS_CONNECTION_ERROR = 8
  • STATUS_SSL_HANDSHAKE_ERROR = 9
  • RESPONSE_CONTINUE = 100
  • RESPONSE_SWITCHING_PROTOCOLS = 101
  • RESPONSE_PROCESSING = 102
  • RESPONSE_OK = 200
  • RESPONSE_CREATED = 201
  • RESPONSE_ACCEPTED = 202
  • RESPONSE_NON_AUTHORITATIVE_INFORMATION = 203
  • RESPONSE_NO_CONTENT = 204
  • RESPONSE_RESET_CONTENT = 205
  • RESPONSE_PARTIAL_CONTENT = 206
  • RESPONSE_MULTI_STATUS = 207
  • RESPONSE_IM_USED = 226
  • RESPONSE_MULTIPLE_CHOICES = 300
  • RESPONSE_MOVED_PERMANENTLY = 301
  • RESPONSE_FOUND = 302
  • RESPONSE_SEE_OTHER = 303
  • RESPONSE_NOT_MODIFIED = 304
  • RESPONSE_USE_PROXY = 305
  • RESPONSE_TEMPORARY_REDIRECT = 307
  • RESPONSE_BAD_REQUEST = 400
  • RESPONSE_UNAUTHORIZED = 401
  • RESPONSE_PAYMENT_REQUIRED = 402
  • RESPONSE_FORBIDDEN = 403
  • RESPONSE_NOT_FOUND = 404
  • RESPONSE_METHOD_NOT_ALLOWED = 405
  • RESPONSE_NOT_ACCEPTABLE = 406
  • RESPONSE_PROXY_AUTHENTICATION_REQUIRED = 407
  • RESPONSE_REQUEST_TIMEOUT = 408
  • RESPONSE_CONFLICT = 409
  • RESPONSE_GONE = 410
  • RESPONSE_LENGTH_REQUIRED = 411
  • RESPONSE_PRECONDITION_FAILED = 412
  • RESPONSE_REQUEST_ENTITY_TOO_LARGE = 413
  • RESPONSE_REQUEST_URI_TOO_LONG = 414
  • RESPONSE_UNSUPPORTED_MEDIA_TYPE = 415
  • RESPONSE_REQUESTED_RANGE_NOT_SATISFIABLE = 416
  • RESPONSE_EXPECTATION_FAILED = 417
  • RESPONSE_UNPROCESSABLE_ENTITY = 422
  • RESPONSE_LOCKED = 423
  • RESPONSE_FAILED_DEPENDENCY = 424
  • RESPONSE_UPGRADE_REQUIRED = 426
  • RESPONSE_INTERNAL_SERVER_ERROR = 500
  • RESPONSE_NOT_IMPLEMENTED = 501
  • RESPONSE_BAD_GATEWAY = 502
  • RESPONSE_SERVICE_UNAVAILABLE = 503
  • RESPONSE_GATEWAY_TIMEOUT = 504
  • RESPONSE_HTTP_VERSION_NOT_SUPPORTED = 505
  • RESPONSE_INSUFFICIENT_STORAGE = 507
  • RESPONSE_NOT_EXTENDED = 510

Description

Hyper-text transfer protocol client. Supports SSL and SSL server certificate verification.

Can be reused to connect to different hosts and make many requests.

Member Function Description

  • void close ( )

Cloces the current connection, allows for reusal of HTTPClient.

Connect to a host. This needs to be done before any requests are sent.

The host should not have http:// prepended but will strip the protocol identifier if provided.

verify_host will check the SSL identity of the host if set to true.

Return current connection.

  • int get_response_body_length ( ) const

Return the response’s body length.

  • int get_response_code ( ) const

Return the HTTP status code of the response.

Return the response headers.

  • Dictionary get_response_headers_as_dictionary ( )

Returns all response headers as dictionary where the case-sensitivity of the keys and values is kept like the server delivers it. A value is a simple String, this string can have more than one value where “; ” is used as separator.

Structure: (“key”:”value1; value2”)

Example: (content-length:12), (Content-Type:application/json; charset=UTF-8)

  • int get_status ( ) const

Returns a STATUS_* enum constant. Need to call poll in order to get status updates.

  • bool has_response ( ) const

Return whether this HTTPClient has a response available.

  • bool is_blocking_mode_enabled ( ) const

Return whether blocking mode is enabled.

  • bool is_response_chunked ( ) const

Return whether this HTTPClient has a response that is chunked.

  • Error poll ( )

This needs to be called in order to have any request processed. Check results with get_status

Generates a GET/POST application/x-www-form-urlencoded style query string from a provided dictionary, e.g.:

var fields = {"username": "user", "password": "pass"}
String queryString = httpClient.query_string_from_dict(fields)
returns:= "username=user&password=pass"

Reads one chunk from the response.

Sends a request to the connected host. The url is what is normally behind the hostname, i.e. in http://somehost.com/index.php, url would be “index.php”.

Headers are HTTP request headers.

To create a POST request with query strings to push to the server, do:

var fields = {"username" : "user", "password" : "pass"}
var queryString = httpClient.query_string_from_dict(fields)
var headers = ["Content-Type: application/x-www-form-urlencoded", "Content-Length: " + str(queryString.length())]
var result = httpClient.request(httpClient.METHOD_POST, "index.php", headers, queryString)

Sends a raw request to the connected host. The url is what is normally behind the hostname, i.e. in http://somehost.com/index.php, url would be “index.php”.

Headers are HTTP request headers.

Sends body raw, as a byte array, does not encode it in any way.

Stub function

Stub function

  • void set_blocking_mode ( bool enabled )

If set to true, execution will block until all data is read from the response.

Set connection to use, for this client.

  • void set_read_chunk_size ( int bytes )

Sets the size of the buffer used and maximum bytes to read per iteration. see read_response_body_chunk