0

私は rest-client-builder プラグイン ( http://grails.org/plugin/rest-client-builder ) を使用しており、ファイルを inputStream オブジェクトとして送信するという問題に直面しました。

プラグインのドキュメントから:

マルチパート リクエストは、リクエスト ボディのプロパティを File、URL、byte[]、または InputStream インスタンスに設定することで可能になります。

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 オブジェクトを送信する方法や、私が間違っていることを誰かが提案できますか?

4

2 に答える 2

1

ファイル タイプの場合、RequestCustomizer で「file」プロパティを設定する必要があります。以下のコードは私のために働いた。

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

def restResponse = rest.post(url) {
   header headerName, headerValue
   contentType "multipart/form-data" 
   setProperty "file", myFile
}
于 2017-04-14T15:44:49.873 に答える