Implementing basic HTTP methods in ECMAScript
You can use the GET/PUT/POST/DELETE HTTP methods from ECMAScript sections in VoiceXML applications. To include these basic HTTP verbs in your VoiceXML applications, you must use the VoiceXML <script> element.
Note: You can also implement other org.apache.http.client.methods in a similar way.
HTTP GET method
Method
org.apache.http.client.methods.HttpGet(Request_uri);
Example
This example demonstrates using the GET method to read an object.
<?xml version="1.0"?>
<!DOCTYPE vxml PUBLIC "-//Nuance/DTD VoiceXML 2.0//EN"
"http://voicexml.nuance.com/dtd/nuancevoicexml-2-0.dtd" >
<vxml version="2.0">
...
<script>
//Create HTTP client object
var httpClient = new org.apache.http.impl.client.HttpClients.createDefault();
//Create HTTP GET method object
var get = new org.apache.http.client.methods.HttpGet("https://jsonplaceholder.typicode.com/posts/1"); get.addHeader("Content-type", "application/json");//Execute the request and collect response
var response = httpClient.execute(get);
var status = response.getStatusLine().getStatusCode()
var line = org.apache.http.util.EntityUtils.toString(response.getEntity(), java.nio.charset.StandardCharsets.UTF_8);
</script>
...
</vxml>
HTTP POST method
Method
org.apache.http.client.methods.HttpPost(Request_uri);
Example
This example demonstrates using the POST method to create an object.
<?xml version="1.0"?>
<!DOCTYPE vxml PUBLIC "-//Nuance/DTD VoiceXML 2.0//EN"
"http://voicexml.nuance.com/dtd/nuancevoicexml-2-0.dtd" >
<vxml version="2.0">
...
<script>
//Create HTTP client object
var httpClient = new org.apache.http.impl.client.HttpClients.createDefault();
//Create HTTP POST method object
var post = new org.apache.http.client.methods.HttpPost("https://jsonplaceholder.typicode.com/posts"); var json_obj = {"userId": 1,"id": 101,"title": "this is random text","completed": false};var json_str = JSON.stringify(json_obj);
var entity = new org.apache.http.entity.StringEntity(json_str);
post.setEntity(entity);
post.setHeader("Content-type", "application/json");//Execute the request and collect response
var response = httpClient.execute(post);
var status = response.getStatusLine().getStatusCode()
var line = org.apache.http.util.EntityUtils.toString(response.getEntity(), java.nio.charset.StandardCharsets.UTF_8);
</script>
...
</vxml>
HTTP PUT method
Method
org.apache.http.client.methods.HttpPut(Request_uri);
Example
This example demonstrates using the PUT method to update an object.
<?xml version="1.0"?>
<!DOCTYPE vxml PUBLIC "-//Nuance/DTD VoiceXML 2.0//EN"
"http://voicexml.nuance.com/dtd/nuancevoicexml-2-0.dtd" >
<vxml version="2.0">
...
<script>
//Create HTTP client object
var httpClient = new org.apache.http.impl.client.HttpClients.createDefault();
//Create HTTP PUT method object
var put = new org.apache.http.client.methods.HttpPut("https://jsonplaceholder.typicode.com/posts/1"); var json_obj = {"userId": 1,"id": 1,"title": "this is random text"};var json_str = JSON.stringify(json_obj);
var entity = new org.apache.http.entity.StringEntity(json_str);
put.setEntity(entity);
put.setHeader("Content-type", "application/json");//Execute the request and collect response
var response = httpClient.execute(put);
var status = response.getStatusLine().getStatusCode()
var line = org.apache.http.util.EntityUtils.toString(response.getEntity(), java.nio.charset.StandardCharsets.UTF_8);
</script>
...
</vxml>
HTTP DELETE method
Method
org.apache.http.client.methods.HttpDelete(Request_uri);
Example
This example demonstrates using the DELETE method to delete an object.
<?xml version="1.0"?>
<!DOCTYPE vxml PUBLIC "-//Nuance/DTD VoiceXML 2.0//EN"
"http://voicexml.nuance.com/dtd/nuancevoicexml-2-0.dtd" >
<vxml version="2.0">
...
<script>
//Create HTTP client object
var httpClient = new org.apache.http.impl.client.HttpClients.createDefault();
//Create HTTP DELETE method object
var del = new org.apache.http.client.methods.HttpDelete("https://jsonplaceholder.typicode.com/posts/1"); del.addHeader("Content-type", "application/json");//Execute the request and collect response
var response = httpClient.execute(del);
var status = response.getStatusLine().getStatusCode()
var line = org.apache.http.util.EntityUtils.toString(response.getEntity(), java.nio.charset.StandardCharsets.UTF_8);
</script>
...
</vxml>