Package: Kernel-HTTP

Class: HTTPClient

Introduction

Environment: container

HTTPClient is a HTTP client library.

It has the following attributes:

  • host the host of the request

  • port the port of the request

  • url the url of the request

  • timeout the timeout of the request

  • contents the contents of the request

  • contentType the content type of the request

  • method the http request method.

It has the following methods:

  • queryAt:put: Add key equals value as an HTTP query parameter to the current request.

  • headerAt:put: Add key equals value as an HTTP header to the current request.

  • execute execute the request

  • get execute a get request

  • put execute a put request

  • post execute a post request

  • delete execute a delete request

  • head execute a head request

You can use it to make HTTP requests to a server.

for example, for a HTTP get:

httpClient := HTTPClient new.
httpClient url: 'http://www.example.com'.
result := httpClient get.

and for a HTTP post:

httpClient := HTTPClient new.
httpClient url: 'http://www.example.com/api'.
httpClient
 contents:
 { ('key1' -> 'key1').
   ('key2' -> 2) } asJson.
result := httpClient post.

The result is a Dictionary which has three keys:

  • status The http status code.

  • headers The http headers.

  • response The http response body. It wil be parsed as JSON if possible, otherwise it will be a string.

For example, you can check the status code like this:

result := httpClient get.
" If the HTTP status code differs from 200, exit the current block. "
(result at: 'status') = 200 ifFalse: [ ^ nil ].

Sometimes, the http request may raise an error for a failure request, we can catch error like this:

error := nil.
[ httpClient get ] onErrorDo: [ :ex | error := ex ].
" If error is not nil, it means the http request failed. "
error ifNotNil: [
  " The error is an APIError object, we can check the error code like this: "
  error code = 'http-request-error'.
  "we can terminate the execution by caret operator.  "
  ^ nil ].

Instance Method

Category: accessing - request

  • contentType - Get the contentType of the HTTPClient.

  • contentType: - Set the contentType of the HTTPClient.

  • contents - Get the contents of the HTTPClient.

  • contents: - Set the contents of the HTTPClient.

  • headerAt:put: - Set key equals value in the HTTP header of the current request.

    For example:

    self headerAt: 'x-conversation-id' put: UUIDGenerator next.
  • host - Get the host of the HTTPClient.

  • host: - Set the host of the HTTPClient.

  • method - Get the method of the HTTPClient.

  • method: - Set the method of the HTTPClient.

  • port - Get the port of the HTTPClient.

  • port: - Set the port of the HTTPClient.

  • queryAt:put: - Add key equals value as an HTTP query parameter to the current request.

    If a key is already present, its corresponding value will be replaced.

    For example:

    self queryAt: 'id' put: '12'.
  • timeout - Get the timeout of the HTTPClient.

  • timeout: - Set the timeout of the HTTPClient.

  • url - Get the url of the HTTPClient.

  • url: - Set the url of the HTTPClient.

Category: operations

  • delete - Execute an HTTP DELETE the request set up and return the response #contents.

  • execute - Execute the currently set up request to generate a response. Return the #contents of the response, if any.

  • get - Execute an HTTP GET the request set up and return the response #contents.

  • head - Execute a HTTP HEAD on the request set up and return nil.

  • post - Execute an HTTP POST the request set up and return the response #contents.

  • put - Execute an HTTP PUT the request set up and return the response #contents.

Class: UUIDGenerator

Introduction

Environment: container

The UUIDGenerator generates UUIDs, this can be used by following the syntax below

"Return a new UUID string."
UUIDGenerator next

Class Method

Category: accessing

  • next - returns a new UUID string.

Last updated