如何使用顶点WebClient(io.vertx.ext.web.client.WebClient)发送多部分表单数据?

时间:2019-06-21 15:23:25

标签: java web webclient vertex

我能够通过io.vertx.ext.web.client.WebClient发送单个文件附件,但不能发送多个文件。

我浏览了文档,探索了WebClient的一些方法,在创建请求时在标头中添加了boundary值,但是没有运气。我通过邮递员提出了同样的要求,并且有效。

此代码有效。它具有单个附件:

MultipartForm multipartForm = MultipartForm.create();
        multipartForm.attribute("description", "test-ticket-description-50");
        multipartForm.attribute("subject", "subject");
        multipartForm.attribute("unique_external_id", "1");
        multipartForm.attribute("status", "2");
        multipartForm.attribute("priority", "2");
        multipartForm.attribute("cc_emails[]","abc@gmail.com");
        multipartForm.attribute("cc_emails[]","cde@gmail.com");
        multipartForm.binaryFileUpload("attachments[]", "Screenshot.png", "/Users/harsit.gupta/Desktop/Screenshot 2019-06-18 at 5.25.56 PM.png", "image/png");

此代码无效。它具有多个附件:

MultipartForm multipartForm = MultipartForm.create();
        multipartForm.attribute("description", "test-ticket-description-50");
        multipartForm.attribute("subject", "subject");
        multipartForm.attribute("unique_external_id", "1");
        multipartForm.attribute("status", "2");
        multipartForm.attribute("priority", "2");
        multipartForm.attribute("cc_emails[]","abc@gmail.com");
        multipartForm.attribute("cc_emails[]","cde@gmail.com");
        multipartForm.binaryFileUpload("attachments[]", "Screenshot.png", "/Users/harsit.gupta/Desktop/Screenshot 2019-06-18 at 5.25.56 PM.png", "image/png");
        multipartForm.binaryFileUpload("attachments[]", "ScreenshotPM.png", "/Users/harsit.gupta/Desktop/Screenshot 2019-06-21 at 3.47.12 PM.png", "image/png");

两个MultipartForm都使用WebClient发送为:

webClient.postAbs(url)
                //.putHeader("Content-type", "multipart/form-data")
                .putHeader("ContentType", "multipart/form-data")
                .putHeader(Constants.AUTHORIZATION, freshdeskAuthHandler)
                .timeout(fresdeskTimeout)
                .sendMultipartForm(multipartForm, httpResponseAsyncResult -> {
                    getFreshdeskResponse(completableFuture, action, httpResponseAsyncResult);
                });

对于前者,状态码为200,而对于后者,状态码为401。我还尝试将attachment[]重命名为attachment1[]attachment2[],但这没有用。同时添加任何一个有效的附件,这意味着文件没有问题。 新鲜的Desk员工用Java提供了this的代码。

期望它适用于任意数量的附件。

0 个答案:

没有答案