使用grails.plugins.rest.client.RestResponse获取img / png

时间:2018-01-05 22:25:33

标签: rest grails png

我正在使用grails.plugins.rest.client.RestResponse从休息端点获取内容类型为png的二进制资源。

import grails.plugins.rest.client.RestBuilder
import grails.plugins.rest.client.RestResponse

String url = "http://localhost:8080/rest-endpoint/image.png"
RestBuilder rest = new RestBuilder()
RestResponse restResponse = rest.get(url){
    auth username, password
    accept "image/png"
    contentType "image/png"
}

byte[] png_image = restResponse.responseEntity.body
println "length " + png_image.length

我不确定为什么返回的长度比预期的少500个字节,我尝试使用不同的图像和不同的网址,并且每次返回的长度都较低。知道为什么吗?

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,最终改用HTTPBuilder

import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

class RestUtil {

    static byte[] getBytes(String url) {
        new HTTPBuilder(url).request(Method.GET, ContentType.BINARY) {
            requestContentType = ContentType.BINARY
            response.success = { resp, binary ->
                return binary.bytes
            }
        }
    }
}

希望有帮助。