HTTPClientScriptable

From Dreamtsoft Wiki
(Redirected from HTTPClientScriptable)
Jump to: navigation, search

Overview

Dreamtsoft provides an HTTP interface for sending GET, POST, PUT and DELETE requests from your Dreamtsoft site.

Examples

The most common usage of the HTTP interface will be for issuing GET or PUT requests to various URLs.

GET and POST Dreamtsoft Users and Groups

var HTTPScriptable = require("core/HTTPScriptable");
var client = new HTTPScriptable("https://www.dreamtsoft.com/"); 
client.setBasicAuthentication("admin", "admin");
client.setRequestMethod("get");
client.addRequestParameter("q", JSON.stringify([[["name","begins","a"]]]));
var userResponse = client.getResponse();
console.log("User query got HTTP code: " + userResponse.getResponseCode());
var userObject = JSON.parse(userResponse.getResponseBody());
for (var i = 0; i < userObject.data.length; i++) {
   console.log("User name begins with a: " + userObject.data[i].name);
}

client.setUrl("https://www.dreamtsoft.com/");
client.addRequestParameter("q", JSON.stringify([[["name","begins","a"]]]));
var groupResponse = client.get();
var groupObject = JSON.parse(groupResponse.getResponseBody());
for (var j = 0; j < groupObject.data.length; j++) {
   console.log("Group name begins with a: " + groupObject.data[j].name);
}

client.setUrl("https://www.dreamtsoft.com/");
client.setRequestBody(JSON.stringify({
   "data":  [
      { "first_name": "Ansel", "last_name": "Adams", "gender": "Male", "user_id": "ansel.adams" }
   ]
}));
var insertResponse = client.post();
console.log("Insert response:\n" + insertResponse.getResponseBody());

Put a record into a web-based tool's "Incident" table

var h = new HTTPScriptable("https://www.dreamtsoft.com/");
h.setBasicAuthentication("joe", "schmoe");
h.setRequestBody(JSON.stringify({
   short_description: "THIS IS A TEST"
}));
h.setRequestMethod('post');
var response = h.getResponse();

console.log(response.getResponseBody());

API Functions

The functions for calling the HTTP client interface are as follows:

setUrl(string)

  • Sets the endpoint for the HTTP request

setRequestMethod(string)

  • post, put, get, or delete

addRequestParameter(string, string)

addRequestParameter(string, object)

  • Adds parameter to the request
  • If object is passed, it will be passed to JSON.stringify()

setBasicAuthentication(username, password)

  • Sets the username and password used to authenticate this request

setRequestBody(string)

setRequestBody(object)

  • Sets the body for the request
  • Commonly used on POST and PUT
  • If object is passed, it will be passed to JSON.stringify()

get()

  • Shortcut to calling setRequestMethod("get") and then invoke()

  h.setRequestMethod('get');
  h.invoke();

post()

  • Shortcut to calling setRequestMethod("post") and then invoke()

  h.setRequestMethod('post');
  h.invoke();

put()

  • Shortcut to calling setRequestMethod("put") and then invoke()

  h.setRequestMethod('put');
  h.invoke();

del()

  • Shortcut to calling setRequestMethod("delete") and then invoke()

  h.setRequestMethod('delete');
  h.invoke();


getResponse()

invoke()