2

I have to implement a Vertx POST request. Via Postman, the request is done as shown in the following picture:

enter image description here

The tricky part is that the server expects the key "upgrade_file" for the body. I could not find out how to do that with Vertx. This is what I have so far:

Buffer bodyBuffer = Buffer.buffer(body); // body is byte[]
HttpClientRequest request = ...
request.handler( response -> ...
request.end(bodyBuffer);

How can I set "upgrade_file" as the key for the body?

René Winkler
  • 6,508
  • 7
  • 42
  • 69

2 Answers2

0

Use a WebClient instead of the HTTP client, it provides dedicated support for submitting forms.

WebClient client = WebClient.create(vertx);

or if you already have created an http client:

WebClient client = WebClient.wrap(httpClient);

Then create the form data as map and send the form using a the right content type

MultiMap form = MultiMap.caseInsensitiveMultiMap();
form.set("upgrade_file", "...");

// Submit the form as a multipart form body
client.post(8080, "yourhost", "/some_address")
      .putHeader("content-type", "multipart/form-data")
      .sendForm(form, ar -> {
        //do something with the response
      });

More example see https://vertx.io/docs/vertx-web-client/java/

Gerald Mücke
  • 10,724
  • 2
  • 50
  • 67
0

If you want to send files, the simplest way is using the sendMultipartForm method of Vertx WebClient.

Create a Multipartform firstly.

MultipartForm form = MultipartForm.create()
  .attribute("imageDescription", "a very nice image")
  .binaryFileUpload(
    "imageFile",
    "image.jpg",
    "/path/to/image",
    "image/jpeg");

Then invoke WebClient.sendMultipartForm(form) to send the request.

The generic Vertx HttpClient is a low-level API, you should searialize the form data into a string or buffer, the format is similar to this example file in Vertx GraphQL testing codes. Then send the buffer to the server side.

Hantsy
  • 8,006
  • 7
  • 64
  • 109