vendredi 21 juin 2019

How to send multipart form data using vertex webclient (io.vertx.ext.web.client.WebClient)?

I able to send a single file attachment through io.vertx.ext.web.client.WebClient but not able to do for multiple files.

I went through the docs, explored some methods of WebClient, added boundary value in header while creating request but no luck. I made the same request via postman and it worked.

This code works. It has single attachment:

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");

This code does not work. It has multiple attachments:

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");

Both the MultipartForm are sent using WebClient as:

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);
                });

For the former the status code is 200 while for the latter the status code is 401. I also tried renaming attachment[] to attachment1[] and attachment2[] but that did not work. Also adding either of the attachment worked, that means there is no issue with the files. Fresh Desk folks gave this code in java which works.

The expectation is that it should work for any number of attachments.




Aucun commentaire:

Enregistrer un commentaire