0

Background

I have two services. Service A is exposed to the outside world and Service B is invoked by Service A. All of these services are dropwizard services. The feature I am trying to implement is to upload a file from frontend which is uploaded to Service A which handles auth. The same request as is should be transferred to Service B. Service B should get the file and save it to disk after compression and thumbnail creation.

I have tested both the services individually using postman and they are able to receive the file as InputStream and the content disposition.

Problem

The problem I am facing is when I want to pass the file in the formData from Service A to Service B.

Here is the code in Service A that sends the form data to Service B.

@POST
@Timed
@Consumes(MediaType.MULTIPART_FORM_DATA)
fun create(
        @Auth user: AuthUser,
        @FormDataParam("file") file: InputStream,
        @FormDataParam("file") fileDetail: FormDataContentDisposition,
        @FormDataParam("file") body: FormDataBodyPart,
        @PathParam("res_id") articleId: String): Response {
    val multiPart = FormDataMultiPart()
    multiPart.bodyPart(body)
    val target = this.client.target("http://localhost:8082/file/create?res_id=1")
    val response = target.request().post(Entity.entity(multiPart, multiPart.mediaType), Response::class.java)
    Response.status(response.status).entity(response.entity).build()
}

Here is the code in Service B that receives it.

@POST
@Path("/create")
@Consumes(MediaType.MULTIPART_FORM_DATA)
fun create(
        @FormDataParam("file") file: InputStream,
        @FormDataParam("file") fileDetail: FormDataContentDisposition,
        @QueryParam("res_id") resId: String): Response {
    resourceLogger.info("Res id in media create $resId")
    upload_file(file, fileDetail)
    return Response.status(201).entity(mapOf("success" to true)).build()
}

The request is reaching Service A and is sent to Service B. But Service B is responding with 400. No logs or stack trace. Couldn't figure out what is going wrong. Any help would be appreciated.

Jebin
  • 702
  • 13
  • 33
  • Created a new FormDataBodyPart object with the input stream converted to byte array and same file content disposition. Didn't work. Same 400. – Jebin Nov 08 '18 at 06:52
  • Check this [working example](https://gist.github.com/psamsotha/f54d2050b3946b2bdc1cb8f94c01f3f0). You need Jersey Test Framework dependency to run it. – Paul Samsotha Nov 08 '18 at 07:09
  • So did you figure it out? – Paul Samsotha Nov 13 '18 at 03:38
  • @PaulSamsotha What I figured out is jersey client created by dropwizard is the issue. When I create a new client and register MultiPartFeature, it works. But when I use the client created by dropwizard in Application class, it doesn't work. Others also reported that. Take a look at this google groups thread. https://groups.google.com/forum/#!topic/dropwizard-user/WeLt4J_dIqs – Jebin Dec 24 '18 at 12:42
  • If DW uses a different connector from the default one, it may be the problem. Some connectors are unable to change the header. So yoh have to manually build the Boundary. – Paul Samsotha Dec 30 '18 at 04:29

0 Answers0