Jersey client POST request using String (mulipart - json + image)

Viewed 28

How can I POST multipart message via String in Jersey? I have String requestBody like:

   --boundaryString
    Content-Type: application/json
    Content-Disposition: form-data; name="meta"
    
    {"id":"FirstParam","count":"SecondParam"}
    --boundaryString
    Content-Type: image/jpeg
    Content-Disposition: form-data; name="content"
    
    яШяа JFIF (image data. content must be binary)
    --boundaryString--

but I get a corrupted image on the server side when I use requestBody.getBytes(). For image converting I use:

BufferedImage bi = ImageIO.read(doc);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(bi, "jpg", baos);

No problem with the server side because I get a good image if I use jersey MultiPart with bearer token:

FormDataMultiPart multiPart = new FormDataMultiPart();
        FormDataBodyPart part1 = new FormDataBodyPart("meta", "{"id":"FirstParam","count":"SecondParam"}", MediaType.APPLICATION_JSON_TYPE);
        multiPart.getBodyParts().add(part1);
        FormDataBodyPart part2 = new FileDataBodyPart("content", doc);
        multiPart.getBodyParts().add(part2);

But for the sign request I need something like:

multipart.getBytes()

But Jersey hasn't methods like this. In a request for a sign, must be use createSignature (with hmac256):

.header("X-Sig", createSignature(requestBody.getBytes()))
.post(Entity.entity(requestBody.getBytes(), mediaType));

createSignature(MUST be in bytes)

0 Answers
Related