We've Moved! Please visit our new and improved forum over at our new portal: https://portal.plumvoice.com/hc/en-us/community/topics

<data> tag REST API - How to check HTTP status Code?

Questions and answers about IVR programming for Plum DEV

Moderators: admin, support

Post Reply
vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

<data> tag REST API - How to check HTTP status Code?

Post by vikas »

When invoking a REST webservice using <data> tag how can I check the HTTP status code returned?

<data name="activationResponse" srcexpr="apiServer+'/accountservices/v1/account/activatecard'" namelist="a b c" method="post"/>

The Plum voice portal mentions below. But that doesn't help to find the HTTP status code.
The requirement is to check the status of HTTP status code and if it is "200" then go to the success branch of call flow.

"The XML data fetched by the <data> element is bound to ECMAScript through the named variable that exposes a read-only subset of the W3C Document Object Model (DOM)."

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: <data> tag REST API - How to check HTTP status Code?

Post by support »

Hi Vikas,

You cannot access the http code from the data tag, but implicitly, any 4xx or 5xx http code will throw an error.badfetch.
If it is not throwing an error.badfetch then you know implicitly that it is returning a 2xx or 3xx code.

Regards,
Plum Support

vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

Re: <data> tag REST API - How to check HTTP status Code?

Post by vikas »

Thanks.

Can I catch specific Badfetch HTTP status code like below? Is this supported by Plum?

<catch event="error.badfetch.https.400 error.badfetch.http.400">
<log label="Error" expr="'HTTP 400 - Bad Request'"/>
<prompt>
This request could not be understood
<break time="1000"/>
</prompt>
<var name="respObj" />
<script>
respObj = {'error':'HTTP 400'};
</script>
<return namelist="respObj" />
</catch>

<catch event="error.badfetch.https.401 error.badfetch.http.401">
<log label="Error" expr="'HTTP 401 - Unauthorized'"/>
<prompt>
This request did not have the proper authentication
<break time="1000"/>
</prompt>
<var name="respObj" />
<script>
respObj = {'error':'HTTP 401'};
</script>
<return namelist="respObj" />
</catch>

<catch event="error.badfetch.https.403 error.badfetch.http.403">
<log label="Error" expr="'HTTP 403 - Forbidden'"/>
<prompt>
This request can not be fulfilled on this server
<break time="1000"/>
</prompt>
<var name="respObj" />
<script>
respObj = {'error':'HTTP 403'};
</script>
<return namelist="respObj" />
</catch>

<catch event="error.badfetch.https.404 error.badfetch.http.404">
<log expr="'HTTP 404 - Not Found'"/>
<prompt>
This request does not match anything on this server
<break time="1000"/>
</prompt>
<var name="respObj" />
<script>
respObj = {'error':'HTTP 404'};
</script>
<return namelist="respObj" />
</catch>


<catch event="error.badfetch.https.500 error.badfetch.http.500">
<log label="Error" expr="'HTTP 500 - Internal Server Error'"/>
<prompt>
The server has experienced an internal error
<break time="1000"/>
</prompt>
<var name="respObj" />
<script>
respObj = {'error':'HTTP 500'};
</script>
<return namelist="respObj" />
</catch>

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: <data> tag REST API - How to check HTTP status Code?

Post by support »

Hello Vikas,

Yes you can catch specific badfetch HTTP status codes as the way you have presented it. We have checked and verified that it does work correctly.

Regards,
Plum Support

vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

Re: <data> tag REST API - How to check HTTP status Code?

Post by vikas »

Thanks.

I was also thinking of invoking a PHP script using the <subdialog> tag and then capturing the HTTP status code in PHP.
That way I can get the exact HTTP status code. Is that possible?

vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

Re: <data> tag REST API - How to check HTTP status Code?

Post by vikas »

I am getting error when trying to invoke a .PHP file.
(this is our Preprod environment & i have masked the URL with *****)

I have verified that the file exists? Is there any specific setting required to access .php file?

Attempting to fetch https://******/Gift_Activation/250902/vxml/webservice.php
HTTP/1.1 405 Method Not Allowed - https://*****/Gift_Activation/250902/vxml/webservice.php
DocumentParser::FetchBuffer - could not open URL: webservice.php
DocumentParser::FetchDocument - exiting with error result 2
errno: 203 uri webservice.php

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: <data> tag REST API - How to check HTTP status Code?

Post by support »

Hello Vikas,

To access the .php and check error code you would want to use the <data> tag. Documentation for this can be found here: http://www.plumvoice.com/docs/dev/voicexml:tags:data

An Example would be the following: <data name="result" src="http://yourservice.com/service.php"/>

Regards,
Plum Support

vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

Re: <data> tag REST API - How to check HTTP status Code?

Post by vikas »

i think you misunderstood me.

I intend to invoke the REST API using PHP that way I can capture the exact HTTP status code from the REST API.
That is why I am planning to use <subdialog> to transition to a new page with PHP code to invoke the REST API.

Does the PHP get executed on our IIS server or does it get executed on Plum Voice Server?
If it gets executed on our IIS server then we will need to get PHP installed . Let me know.

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: <data> tag REST API - How to check HTTP status Code?

Post by support »

Hi Vikas,

If the script (subdialog in this case) is located on your web servers, that's where the code will be executed. So when the platform encounters the subdialog tag, or any script referenced by a url for that matter, what it does is simply attempt to fetch valid vxml at the supplied url. This means that it will make either a GET or POST request to the script (depending on what you specify) and the remote server will execute the request and return the vxml from the subdialog. From the sound of it, if the php code is in your subdialog and the subdialog is served up from your web server, then yes, you will need to enable php on your web server.

This is a very common scenario to use a high level language for web service integration when http codes and headers are required or important. It sounds like you've got this figured out for the most part, but here's a very basic idea of how you'd get this working. Let's pretend we've got a script that's looking up a callers first name based on the callers phone number. First we'd make a request to the subdialog script and pass the phone_number parameter. Then, in the subdialog, the first thing that will get executed is the php at the top of the file before the vxml is returned. In the subdialog is where you'd integrate with your api in your high level language and then return a namelist of data being the variables you wish to return from your subdialog. We've just made a basic sample subdialog that assumes we're integrating with a json api and returning the http code and a first_name variables from our api request back to our vxml script.

main.vxml

Code: Select all

<form id="lookup_info">
	<subdialog name="caller_info" src="sub_lookup_caller.php" namelist="phone_number" method="post" fetchtimeout="60s"/>
	<block>
		<if cond="caller_info.httpd_code == 200">
			<!-- caller found case -->
		<elseif cond="caller_info.httpd_code == 404"/>
                        <!-- caller not found case -->
		<else/>
			<!-- some default case -->
		</if>
	</block>
</form>
sub_lookup_caller.php

Code: Select all

<?php
header('Content-type: text/xml');
// this code will get executed before the vxml data is returned to the platform

// get your posted params
$phone_number = $_POST['phone_number'];

// here you can use curl or whatever you need to integrate with the api
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://somewhere.com/lookup_script.php?'.http_build_query(['phone_number' => $phone_number]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

$json = json_decode($result, TRUE);
$first_name = $json['first_name'];

echo("<?xml version=\"1.0\"?>\n");
?>
<vxml version="2.0">

<form>
	<block>
		<var name="http_code" expr="<?= $http_code ?>"/>
                <var name="first_name" expr="'<?= $first_name ?>'"/>
		<return namelist="http_code first_name"/>
	</block>
</form>

</vxml>
Hopefully that help, but please let us know if you have any further questions.

Regards,
Plum Support

vikas
Posts: 53
Joined: Wed May 13, 2015 7:46 pm

Re: <data> tag REST API - How to check HTTP status Code?

Post by vikas »

Thanks so much for the details about how to use PHP.

However I was wondering if we can achieve same using Javascript.
Is it possible to use JavaScript to invoke REST API & capture HTTP status code?

That way we won't need server side scripting / PHP.
Also let me know if <VXML> supports Javascript libraries like jQuery, Angular.js etc

support
Posts: 3632
Joined: Mon Jun 02, 2003 3:47 pm
Location: Boston, MA
Contact:

Re: <data> tag REST API - How to check HTTP status Code?

Post by support »

Hi Vikas,

That's a good point. Unfortunately the platform doesn't honor http request through javascript, so the server level is the appropriate place for things of this nature. The platform does support native javascript so you can use javascript by default. We haven't tried utilizing jquery and angular within vxml, but you can use the <script> tag to include javascript files in your vxml files, so you can certainly try it out and see if it does what you're looking to accomplish.

Regards,
Plum Support

Post Reply