Rest API GET response with binary data

2.3k views Asked by At

I am working on Restful API, in memory we have a Json-format object, customer needs to download this json object with zip file format.

Now I am using Vertx to implement this GET response, the response needs to return a ZIP-format binary data, basically it looks like the following,

    JsonObject jsonObject = new JsonObject();
    jsonObject.put("a", "a1")
              .put("b", "b1");


    routingContext
        .response()
        .setStatusCode(200)
        .putHeader("Content-Type", "application/zip")
        .....
        .....

I have no idea how to implement it with Vertx routingContext, could anybody give points or some example code in order to implement it?

1

There are 1 answers

0
ph0enix On BEST ANSWER

I think the only item you are missing in the method chain is the last line: .end(...);

This method is overloaded multiple times. For the case up here, the method would look like .end(Json.encode(jsonObject)); with jsonObject converted to String. I assume this is only the example since this would return the zip file but one that cannot be opened with zip archive software (because it's a JSON obviously).

end(...) method also accepts Buffer from the io.vertx.core.buffer.Buffer package, maybe that is what you are looking for - turning the data you have into Buffer and serving it.