StackHub Bulletin BoardAdvertise your announcements here!
API

afHttpClientExt

Axon functions for making HTTP requests to REST APIs
afHttpClientExtAxon funcs

Registered StackHub users may elect to receive email notifications whenever a new package version is released or a comment is posted on the forum.

There are 0 watchers.

v2.0.2

afHttpAuth
afHttpAuth(authScheme, username, password: null)

Forces authentication to be performed as part of the next HTTP request. Valid built in authentication schemes are:

  • basic
  • bearer (leave password as null)
  • digest
  • haystack

If password is a Ref, then it is used to retrieve the real password from Folio's secure password store.

Credentials need only be set once per session.

Example:

afHttpAuth("basic", "user", @xxxx-xxxx)
afHttpGet(`http://example.com/secret.txt`)

afHttpAuth("bearer", "xxxx-xxxx")
afHttpGet(`http://example2.com/some-other-secret.txt`)

Need a custom authentication scheme for Http Client? Ask Fantom Factory to write it for you?

Fantom signature:
@Axon static Void afHttpAuth(Str? authScheme, Str username, Obj? password := null)
afHttpClear
afHttpClear(opts: null)

Resets the HTTP Ext back to an original (pre-session) state.

To only clear the session (clear cookies and auth credentials), pass a session flag:

afHttpClear({session})

To clear the default behaviour for a HTTP response status code, pass in the exact code, or a glob string for wildcards:

// do not error on 404 - Not Found status codes
afHttpClear({statusCode:404})

// do not follow redirects
afHttpClear({statusCode:"3xx"})
Fantom signature:
@Axon static Void afHttpClear(Dict? opts := null)
afHttpCookies
afHttpCookies()

Returns a Grid of HTTP cookies stored in the current HTTP session. Use afHttpClear to clear them.

Fantom signature:
@Axon static Grid afHttpCookies()
afHttpDebug
afHttpDebug(on: true)

Turns debugging on / off. When on, all HTTP request / response data will be logged to the extension and printed to the console.

For security reasons this function is for super users only.

Fantom signature:
@Axon { su=true; } static Void afHttpDebug(Bool on := true)
afHttpGet
afHttpGet(url, headers: null, body: null)

Makes a HTTP GET request to the given URL and returns the response body as a string. Note that body's aren't usually sent in GET requests.

Parameters:

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

The body may be any object as described in Body Sniffing.

Examples:

afHttpGet(`http://example.com`)
afHttpGet(`http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon static Str afHttpGet(Uri url, Dict? headers := null, Obj? body := null)
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 (Obj) - the request body (optional)

The body may be any object as described in Body Sniffing.

Examples:

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

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

Parameters:

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

The body may be any object as described in Body Sniffing.

Examples:

afHttpRequest("GET", `http://example.com`)
afHttpRequest("PUT", `http://example.com`, { "Content-Type" : "text/plain" }, "Hello Mum!")
Fantom signature:
@Axon static Str afHttpRequest(Str method, Uri url, Dict? headers := null, Obj? body := null)
afHttpResponse
afHttpResponse(opts: null)

Returns a Dict of the last HTTP response.

Example response:

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

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

To avoid this, you can pass in "safeNames" as a option (similar to ioReadJson) and the key names will be converted to SkySpark Grid safe tag names.

Example safe headers:

afHttpResponse({safeNames})->headers  // --> {"contentLength" : 320}

afHttpResponse needs to be run in a function, and will return null if run directly in the Tools app.

Fantom signature:
@Axon static Dict? afHttpResponse(Dict? opts := null)
afHttpWriteToJsonStr
afHttpWriteToJsonStr(jsonObj, opts)

Writes the given Axon object to a JSON string.

The Dict is converted using standard JSON notation and NOT Haystack JSON notation. Therefore afHttpWriteToJsonStr() replaces the core ioWriteJson() function when communicating with non-Haystack REST APIs.

This function only supports writing the following types:

  • Dict
  • List
  • null
  • Bool
  • Number (without units)
  • Str

Attempting to write an unsupported type results in an error.

Supported options are:

  • prettyPrint - Marker - Turns on pretty printing. (defaults to off)
  • indent - String - the string to use for indenting. (defaults to 2 spaces)
  • maxWidth - Number - Sets a max width for pretty printed strings. (defaults to 80)
  • lenient - Marker - ignores units in numbers, and unknown types are converted via toStr() and written as strings.

Note that Lists and Dict will only be pretty printed if they exceed maxWidth.

Example:

afHttpWriteToJsonStr({name:"Emma", score:11}, {prettyPrint, indent:"  ", maxWidth:20})
Fantom signature:
@Axon static Str afHttpWriteToJsonStr(Obj? jsonObj, Dict? opts)
Published by Fantom Factory

Products & Services by Fantom Factory

Packages by Fantom Factory

Commercial packages

Free packages