a!httpWrite() Function

Makes an HTTP request. This function returns a writer and must be used with the bind() function.

Syntax

a!httpWrite( url, method, headers, queryParameters, basicAuthenticationCredentials, body)

url (Text): The URL for the request.

method (Text): The http method with which to call the url.

headers (List of HttpKeyValuePair): A list of headers you wish to send as part of the request. Built with a!httpHeader().

queryParameters (List of HttpKeyValuePair): A list of query parameters you wish to add to the request. Built with a!httpQueryParameter().

basicAuthenticationCredentials (HttpAuthenticationBasic): The credentials for the request. Built with a!httpAuthenticationBasic().

body (Any Type): The request body.

Returns

Writer

This function returns a writer and must be used with the bind() function.

Examples

Read From and Write To a Web API From Within a SAIL Interface

Instacount.io is a website that provides a web API that lets you increment and decrement a counter via HTTP requests. You can find more details on the Instacount web API at http://instacount.io. Note that this example is using an account tier that is limited to 1000 requests per month. If it is not working, set up your own free account on Instacount and replace the application id and API key with your unique values.

Copy and paste the expression into the Interface Designer to test it.

load(
  local!countUrl: "https://api.instacount.io/sharded_counters/example-counter",
  local!commonHeaders: { 
    /* Accept header, API Key and Application Id as prescribed 
       by the Instacount API documentation */
    a!httpHeader(
      name: "Accept",
      value: "application/vnd.instacount.v1+json"
    ),
    a!httpHeader(
      name: "X-Instacount-API-Key",
      value: "cp7ptmqcut5ufpdvt4nm4er4n0igcs5hgpriuuf7nfldvpuosm8f"
    ),
    a!httpHeader(
      name: "X-Instacount-Application-Id",
      value: "91f8ACL6QEqg5dws5fAn4A"
    )
  },
  local!count: bind(
    get: a!fromJson(
      jsonText: a!httpQuery(
        url: local!countUrl,
        headers: local!commonHeaders
      ).result.body
    ).count,
    set: a!httpWrite(
      /*The Instacount API has separate URLs for incrementing and decrementing
        the counter, so we save the correct URL depending on which
        button the user clicks.*/
      url: _, 
      method: "POST",
      headers: append(
        local!commonHeaders,
        {
          a!httpHeader(
            name: "Content-Type",
            value: "application/vnd.instacount.v1+json"
          )
        }
      ),
      body: a!toJson(
        {amount: 1, async: false}
      )
    )   
  ),
  a!dashboardLayout(
    firstColumnContents: {
      a!textField(
        label: "Counter",
        instructions: "Use the +1 and -1 buttons below to increase or decrease the counter above",
        value: local!count,
        readOnly: true
      ),
      a!buttonLayout(
        secondaryButtons: {
          a!buttonWidget(
            label: "- 1",
            value: local!countUrl & "/decrements",
            saveInto: local!count
          ),
          a!buttonWidget(
            label: "+ 1",
            value: local!countUrl & "/increments",
            saveInto: local!count
          )
        }
      )
    }
  )
)

See also:

FEEDBACK