We already have an http client object that can be retieved via WGA.createHttpClient(). This object is described here.
However it is very complicated to use this from TMLScript.
We now added a wrapper around this http-client to make it more easy to use.
Create the new http client object via
var client = WGA.HttpClient(url)
Available methods on this object are
- get([callback])
- delete([callback])
- post(data [, contenttype[, charset]][, callback])
- put(data [, contenttype[, charset]][, callback])
For put/post the default contenttype is "text/plain" and default charset is the configured OpenWGA output encoding (normally UTF-8).
The return value of get/delete/post/put is the HTTP-Status code as integer.
In addition we implemented:
- setRequestHeader(key, value)
- setRequestHeaders(map)
- setCredentials(username, password)
setRequestHeader/s() and setCredentials() returns the client object so the methods can be chained with other methods.
get/delete/post/put accepts an optional callback function to read the received data. This callback function is called with a Result object with the following properties:
- status (the http status code)
- statusText (status code as text info)
- text (the result as String)
- inputStream (an inputstream to read the result as binary data).
- contentType (the content type of the result)
- getHeader(headername) - returns a defined response header value
Samples:
// a simple get request
WGA.HttpClient("http://some-domain.com").get(function(result){
if(result.status==200)
console.log("result is", result.text);
else console.error("HTTP Error", result.statusText)
})
// a simple post request
WGA.HttpClient("http://some-domain.com")
.setCredentials("walter", "wichtig")
.setRequestHeaders({
"User-Agent": "Walters HTTPClient/1.0"
})
.post("Hallo Düsseldorf", "text/plain", "latin1")