afHttpExt

Axon functions for making HTTP requests to REST APIs
afHttpExt

Registered users on StackHub may elect to receive email notifications whenever new package versions are released.

v1.0.6

Axon functions

afHttpBasicAuth
afHttpBasicAuth(username: null, password: null)

Generates a BASIC authentication HTTP header value from the given credentials.

Example:

afHttpBasicAuth("user", "pencil")  // --> "Basic dXNlcjpwZW5jaWw="
Fantom signature:
@Axon const static Str afHttpBasicAuth(Str? username := null, Str? password := null)
afHttpDelete
afHttpDelete(url, headers: null, body: null)

Makes a HTTP DELETE request to the given URL and returns the response body as a string.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • body (Str) - the request body (optional)

Examples:

afHttpDelete(`http://example.com`)
afHttpDelete(`http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon const static Str afHttpDelete(Uri url, Dict? headers := null, Str? body := null)
afHttpGet
afHttpGet(url, headers: null, body: null)

Makes a HTTP GET request to the given URL and returns the response body as a string.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • body (Str) - the request body (optional)

Examples:

afHttpGet(`http://example.com`)
afHttpGet(`http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon const static Str afHttpGet(Uri url, Dict? headers := null, Str? body := null)
afHttpLastRes
afHttpLastRes()

Returns the response from the last HTTP request, if any.

Note that displaying the HTTP response headers in a Grid may cause an Invalid col name error as per the SkySpark forum post: Displaying Axon Dicts with non-standard keys

Fantom signature:
@Axon const static Dict? afHttpLastRes()
afHttpPost
afHttpPost(url, headers: null, body: null)

Makes a HTTP POST request to the given URL and returns the response body as a string.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • body (Str) - the request body (optional)

Examples:

afHttpPost(`http://example.com`)
afHttpPost(`http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon const static Str afHttpPost(Uri url, Dict? headers := null, Str? body := null)
afHttpPostFile
afHttpPostFile(url, headers, file)

Makes a HTTP POST request to the URL with the given file. The Content-Type HTTP header is set from the file extension's MIME type, or application/octet-stream if unknown.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • file (Uri) - name / value pairs (required)

Example:

afHttpPostFile(`http://example.com`, `io/upload.zip`)

Note to mimic a file upload from a web page, use afHttpPostMultipartForm() instead.

Fantom signature:
@Axon const static Str afHttpPostFile(Uri url, Dict? headers, Uri file)
afHttpPostForm
afHttpPostForm(url, headers, form)

Makes a HTTP POST request to the URL with the given form data. The Content-Type HTTP header is set to application/x-www-form-urlencoded.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • form (Dict) - name / value pairs (required)

Examples:

afHttpPostForm(`http://example.com`, { "id" : 6666, "name": "Steve" })
afHttpPostForm(`http://example.com`, { "id" : 6666, "name": "Steve" }, { "Authorisation" : "BASIC xxxx" })
Fantom signature:
@Axon const static Str afHttpPostForm(Uri url, Dict? headers, Dict form)
afHttpPostMultipartForm
afHttpPostMultipartForm(url, headers, fn)

Makes a HTTP POST request to the URL with the multipart form contents defined by the given func.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • fn (Func) - call to add form parts

Example:

afHttpPostMultipartForm(`http://example.com`, null) (addPart) => do
    addPart("text", "name", "Steve")
    addPart("file", "file", `io/upload.zip`)
end

The paramaters for addPart() are:

  • type (Str) - the type of part, maybe one of: text, file (required)
  • name (Str) - name of the form part (required)
  • value (Str/Uri) - value of the form part (required)
  • headers (Dict) - part headers (optional)

When type is text, then value must be a Str and the Content-Type part header is set to text/plain.

When type is file, then value must be a Uri that points to the file to upload. The Content-Type part header is set from the file extension's MIME type, or application/octet-stream if unknown.

Fantom signature:
@Axon const static Str afHttpPostMultipartForm(Uri url, Dict? headers, Fn fn)
afHttpPut
afHttpPut(url, headers: null, body: null)

Makes a HTTP PUT request to the given URL and returns the response body as a string.

Parameters:

  • url (Uri) - the URL (required)
  • headers (Dict) - HTTP request headers (optional)
  • body (Str) - the request body (optional)

Examples:

afHttpPut(`http://example.com`)
afHttpPut(`http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon const static Str afHttpPut(Uri url, Dict? headers := null, Str? body := null)
afHttpSendReq
afHttpSendReq(req)

A general purpose function that makes a generic HTTP request. It takes a Dict of optional request parameters and returns a Dict of response parameters.

Example request:

afHttpSendReq({
    "url"     : `http://example.com`,  // Uri  - required
    "version" : "1.1",                 // Str  - optional, defaults to "1.1"
    "method"  : "POST",                // Str  - optional, defaults to "GET"
    "headers" : {"Auth" : "secret"},   // Dict - optional, defaults to {:}
    "body"    : "stuff",               // Str  - optional, defaults to null
    "options" : {"connectTimeout" : 2} // Dict - optional, defaults to {:}
})

headers is a Dict of strings and may contain multiple values.

Available options:

{
    "followRedirects" : false,  // Bool            - optional, defaults to true
    "connectTimeout"  : 30sec,  // Duration number - optional, defaults 1min
    "receiveTimeout"  : 30sec   // Duration number - optional, defaults 1min
}

Example response:

{
    "url"        : `http://example.com`,
    "statusCode" : 200,
    "statusMsg"  : "OK",
    "headers"    : {"Content-Length" : 320},
    "version"    : "1.1",
    "body"       : "<html><head><title>..."
}

Note that afHttpSendReq() returns normally regardless of the HTTP response code.

Fantom signature:
@Axon const static Dict afHttpSendReq(Dict req)
Published by Fantom Factory

Products & Services by Fantom Factory

Packages by Fantom Factory

Commercial packages

Free packages