VBScript → Lua SwxWare v12.40
This object can be used to perform any kind of web requests from within the call routing.
Dim oWebRequest, respCode, oHeaders, oHeader, iIdx Set oWebRequest = PBXScript.WebRequest oWebRequest.HttpVerb = HttpVerbGet oWebRequest.URL = "https://www.example.org" oWebRequest.AddHeader "Content-Type:application/text" respCode = oWebRequest.Execute PBXScript.OutputTrace "Execute result=" & respCode & " -> " & oWebRequest.ResponseBody PBXScript.Sleep 1000 ' Dump response headers Set oHeaders = oWebRequest.ResponseHeaders iIdx = 0 For Each oHeader In oHeaders PBXScript.OutputTrace "---> header[" & iIdx & "] = " & oHeader iIdx = iIdx + 1 Next oWebRequest.Reset
The PBXScript.WebRequest object provides the following properties and methods:
AddHeader
This method takes one string parameter. It sets the given parameter as HTTP header, e.g.:
oWebRequest.AddHeader "Content-Type:application/text"
or
oWebRequest.AddHeader "Content-Type:application/json; charset=UTF-8"
AddQueryPair
This method takes two string parameters. It sets the given parameters as query data (e.g. in an HTTP POST request). The given parameters will get properly escaped, so no own character escaping is needed. E.g.:
oWebRequest.AddQueryPair("user", "Dörthe, Weiß")
HttpVerb
This property defines the HTTP request type. If omitted, the listed default value will be used. The following values are possible.
- HttpVerbGet (Default)
- HttpVerbPost
- HttpVerbPut
- HttpVerbDelete
- HttpVerbPatch
- HttpVerbHead
HttpAuth
This property defines the HTTP authentication at the given destination (URL). If omitted, the listed default value will be used. The following values are possible.
- HttpAuthNone (Default)
- HttpAuthBasic
- HttpAuthDigest
- HttpAuthBearer
- HttpAuthAny
AuthUser / AuthPass
This properties take string values of the user name/id and password/token if an HttpAuth authentication is used that requires these values.
oWebRequest.HttpAuth = HttpAuthBasic oWebRequest.AuthUser = "jdoe@example.com" oWebRequest.AuthPass = "pa$$w0rd"
Both properties will be taken to form a properly escaped "authentication" header (AuthUser:AuthPass). From the above example:
~GET / HTTP/2
~Host: example.org
~authorization: Basic amRvZUBleGFtcGxlLmNvbTpwYSQkdzByZA==
~user-agent: IpPbxScrWeb
~accept: */*
Hint: if a username/token:xxxxxxxxx authentication is required, you need to form it like this:
oWebRequest.AuthUser = "jdoe@example.com/token"
oWebRequest.AuthPass = "xxxxxxxxxxxx"
UrlEncode
The method takes one string parameter. It URL encodes the given string and returns it.
Dim encodedURL encodedURL = oWebRequest.UrlEncode "Dörthe, Weiß sucht das Glück"
The returned encoded string will look like this
D%C3%B6rthe%2C%20Wei%C3%9F%20sucht%20das%20Gl%C3%BCck
VerifyPeer
This property takes a boolean value. It activates (True, Default) or deactivates (False) the SSL/TLS certificate validation. It is not recommended to disable it.
URL
This property takes a string value. It defines the URL for the HTP request.
oWebRequest.HttpVerb = HttpVerbGet oWebRequest.URL = "https://example.org"
RequestBody
This property takes a string value. It can be used to provide request body like, e.g. JSON formatted request information:
oWebRequest.RequestBody = _ "{" & _ " ""ticket"":" & _ " {" & _ " ""comment"":" & _ " {" & _ " ""body"": %COMMENT%," & _ " ""author_id"": %AUTHORID%," & _ " ""public"": " & %PUBLIC% & _ " }" & _ " }" & _ "}"
Execute
This method performs the HTTP request being defined by all above listed properties and methods and returns the response code (as integer value).
if oWebRequest.Execute = 200 then
'...
end if
ResponseBody
This property is a string value. It contains the response data of the request.
if oWebRequest.Execute = 200 then
PBXScript.OutputTrace oWebRequest.ResponseBody
end if
ResponseHeaders
This property is a string list value. It contains all returned response headers. The following code is an example to dump all headers:
Dim oHeader, oHeaders
Set oHeaders = oWebRequest.ResponseHeaders
iIdx = 0
For Each oHeader In oHeaders
PBXScript.OutputTrace "---> header[" & iIdx & "] = " & oHeader
iIdx = iIdx + 1
Next
Reset
This method resets all previously set data and can be used to prepare the PBXScript.WebRequest object for the next request.
Beside the above given examples the Zendesk Integration project (from version 1.3.0 on) makes use of the PBXScript.WebRequest object and demonstrates the usage of JSON formatted request and response data from a cloud based REST API service.
There is also a function LaunchHTTPRequestEx from the Function Collection available which demonstrates the usage of the PBXScript.WebRequest object.
By Tom Wellige