04-05-2018 05:32 AM
Hi,
I want to check if an audio file exists (without actually playing it) in a Call Studio App.
Has anyone created some custom HTTP Get Code to do something similar?
i.e. Check if File exists. If you get a If 200 or 304 you know its there.
If you get a 404 you know its not.
Why I would like it
This will then allow me to state before doing a record prompt - that the prompt already exists and you can can ask the user to confirm if they want to overwrite the existing file.
Gerry
Solved! Go to Solution.
04-05-2018 05:46 AM
If you have 11.0 or higher, you can try using the RestClient element to
make the http request for the audio file.
Then look at the element data status_code - to see if you get 200 or 404.
I just tried this and it worked perfectly!
Although, just to be sure, you may want to set the ReadTimeout or
ConnectTimeout shorter than 5000ms, and you may want to catch . Java
Exceptions, just in case the media server doesn't return anything in time.
04-05-2018 05:46 AM
If you have 11.0 or higher, you can try using the RestClient element to
make the http request for the audio file.
Then look at the element data status_code - to see if you get 200 or 404.
I just tried this and it worked perfectly!
Although, just to be sure, you may want to set the ReadTimeout or
ConnectTimeout shorter than 5000ms, and you may want to catch . Java
Exceptions, just in case the media server doesn't return anything in time.
04-05-2018 06:25 AM
Janine - brilliant idea! Thanks.
Gerry
04-05-2018 06:20 AM
HTTP HEAD method is the way to test existence and optionally get the last-modified header info.
This sort of thing --
HttpURLConnection con = (HttpURLConnection) new URL("http://cvpsvr:7000/CVP/audio/holdmusic.wav").openConnection();
con.setRequestMethod("HEAD");
System.out.println(con.getResponseCode());
System.out.println(con.getHeaderField("Last-Modified"));
con.disconnect();
04-05-2018 06:28 AM
Paul,
I can see that your solution might be better technically as it would not download the actually audio file (or would it? - I would have to check via wireshark), but Janine's REST Client approch is "out of the box". Do you see any issue with using the REST Client for this use case, I don't see why not?
Gerry
04-05-2018 06:46 AM
Gerry,
Correct, it does what you were asking but doesn't download any response body. It also lets you get at the headers if you need that. REST client element doesn't unfortunately include HEAD method so would do the job but would also retrieve the audio file content. So, you're right that HEAD is technically better but less convenient.
Paul
04-05-2018 07:00 AM
Paul, Janine,
Thanks to you both. I wish I could answer both your answers correct. As they both are. I might get around to creating a proper version (using Pauls method) and if I do I will post here... But Janine you get a gold star for thinking outside the box and having the solution inside the box. Thanks a mil.
Gerry
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide