Search code examples
restcontent-typerest-client

How to handle response Content Type Audio when accessing REST API?


Am using Voice RSS to do text to speech translation for my java application. I have used clients like RESTY in my past to handle simple json requests which am comfortable with. But in this case, the server (Voice RSS) is returning content type as audio, am unsure how to handle and unwrap this as a java client. Any help would be greatly appreciated

Thanks Karthik


Solution

  • I figured out that you can use Url and BufferedInputStream to download the response from the web service as a byte array or as an OutputStream which you can save it as anything.

    InputStream in = null;
            ByteArrayOutputStream outputStream = null;
            byte[] byteArray = null;
            try {
                URL link = new URL("YOUR_URL");
                in = new BufferedInputStream(link.openStream());
                outputStream = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int n = 0;
                while (-1 != (n = in.read(buf))) {
                    outputStream.write(buf, 0, n);
                    }
                byteArray = outputStream.toByteArray();
            }catch (Exception ex){
                throw ex;
            }finally {
                if(in != null) in.close();;
                if(outputStream != null) outputStream.close();
            }