rest - 无法使用Grails Rest-Client-Builder发送InputStream对象

标签 rest grails grails-plugin

我一直在使用rest-client-builder插件(http://grails.org/plugin/rest-client-builder)并遇到发送文件作为inputStream对象的问题。

从插件文档中:

Multipart requests are possible by setting properties of the request body to File, URL, byte[] or InputStream instances:



def resp = rest.post(url) {
        contentType "multipart/form-data"
        zip = new File(pluginPackage)
        pom = new File(pomFile)
        xml = new File(pluginXmlFile)
    }

我的代码:
def post(String url, InputStream photo, String contentType, Cookie[] cookies = null) {
    def rest = new RestBuilder()

    def cookiesHeaderString = ""
    if (cookies) {
        cookiesHeaderString = WebUtils.buildCookiesHeader(cookies)
    }

    def resp = rest.post(url) {
        header "Cookie", cookiesHeaderString
        file = photo
        contentType "multipart/form-data"
    }

    return resp?.responseEntity?.body
}

有人可以建议如何发送InputStream对象或我做错了什么吗?

最佳答案

对于文件类型,我们需要在RequestCustomizer上设置“file”属性。下面的代码为我工作。

File myFile = new File("myFile.txt")

def restResponse = rest.post(url) {
   header headerName, headerValue
   contentType "multipart/form-data" 
   setProperty "file", myFile
}

关于rest - 无法使用Grails Rest-Client-Builder发送InputStream对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18770705/

相关文章:

java - Jersey:将值从 ContainerRequestFilter 传递到端点

api - 如何在 REST API 中返回资源计数?

hibernate - Grails 域对象没有被持久化

Grails uglifyjs 插件不起作用

grails - Grails应用程序中用于电子邮件提取的插件

grails - 如何让 Grails 从 Maven 存储库中获取最新的本地 jars?

java - Rest Api Post 请求

R、GET 和 GZ 压缩

spring - 如何在 Grails 电子邮件服务中获取自定义 "from address"

grails - 如何通过其他grails应用程序重用grails应用程序(代码和数据)?