grails-plugins / grails-rest-client-builder

REST client plugin that uses Spring's RestTemplate
http://grails.org/plugin/rest-client-builder
Apache License 2.0
65 stars 32 forks source link

Download file using RestBuilder #15

Open jojopad opened 11 years ago

jojopad commented 11 years ago

It would be great to provide support for downloading a file via an InputStream when using the RestBuilder class.

richardwooding commented 8 years ago

I second this. I'm using RESTBuilder to communicate with JSON-based REST services, but it's failing when I try to download binary files.

MLoenhardt commented 8 years ago

Would be great for us.

JohnTheBeloved commented 8 years ago

Need this just now

Philip-Wu commented 7 years ago

Here's a workaround on how to do this. I also wrote up more detailed description on my blog:

Blog link

    def void download(String url, OutputStream outputStream) {
        logger.info("url="+url)

        RequestCallback requestCallback = new RequestCallback() {
            void doWithRequest(ClientHttpRequest clientRequest) throws IOException {
                clientRequest.headers.add('accept', 'application/octet-stream')
            }
        }

        ResponseExtractor responseExtractor = new ResponseExtractor() {
            Object extractData(ClientHttpResponse clientResponse) throws IOException {
                System.out.println('Headers: '+clientResponse.headers)
                outputStream << clientResponse.body
            }
        }

        RestBuilder rest = new RestBuilder()
        RestTemplate restTemplate = rest.getRestTemplate()
        restTemplate.execute(url, HttpMethod.GET, requestCallback, responseExtractor)                       
    }