Reputation: 699
I'm working with two Java containers where the first container (Client) sends a list of images to a service exposed by the second container (Server) using a multipart POST request. However, the server always responds with a 415 "Unsupported Media Type" error.
Here is the code for the client:
public class ClientController {
public List<...> doRequest(List<InputStream> images, Integer value1, double value2) {
try (MultiPart multiPart = new FormDataMultiPart()) {
Client client = ClientBuilder
.newBuilder()
.register(MultiPartFeature.class)
.build();
WebTarget target = client.target(baseURI + "/service/value1/" + value1 + "/value2/" + value2);
int index = 0;
for (InputStream image : images) {
StreamDataBodyPart bodyPart = new StreamDataBodyPart("file" + index, image);
multiPart.bodyPart(bodyPart);
index++;
}
Response response = target.request().post(Entity.entity(multiPart, multiPart.getMediaType()));
if (response.getStatus() != Response.Status.OK.getStatusCode()) {
// Handle error
}
}
}
}
And here is the server code that receives and processes the request:
@Path("service")
public class ServerController {
@POST
@Path("/value1/{value1}/value2/{value2}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response getResponse(@PathParam("value1") int value1, @PathParam("value2") double value2, MultiPart multiPart) {
List<InputStream> images = multiPart
.getBodyParts()
.stream()
.map(bodyPart -> bodyPart.getEntityAs(InputStream.class))
.collect(Collectors.toList());
// Process images and generate response
}
}
I have already registered the MultiPartFeature on the client side, but the server still responds with a 415 error. What could be the cause of this issue? How can I correctly send a list of images as a multipart POST request from the client to the server?
Any help would be appreciated. Thanks!
Upvotes: 0
Views: 51