0

How to send a base64 image in postman json format. I was added image file in postman- body form data and it's encode details in json format but i got a bad request only in my attempts. [JSON parse error: Cannot deserialize instance of byte[] out of START_OBJECT token]. found that there is no error in source but the actual issue in json format in postman. SOURCE: https://github.com/arun0009/ocr-tess4j-rest

I applied a stack overflow solution but it's repeating the same error. I was adding [], {} in my json formats from stack overflow suggestion types.

CLASS:

public class Image  {
    private String id;
    private String userId;
    private byte[] image;
    private String extension;
    private String text;  }

CONTROLLER :

@RequestMapping(value = "ocr/v1/upload", method= RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public Status doOcr(@RequestBody Image image) throws Exception {
try {      ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decodeBase64(image.getImage()));
            Tesseract tesseract = new Tesseract(); // JNA Interface Mapping
            String imageText = tesseract.doOCR(ImageIO.read(bis));
            image.setText(imageText);
            repository.save(image);
            LOGGER.debug("OCR Result = " + imageText);
     } catch (Exception e) {
   LOGGER.error("TessearctException while converting/uploading image: ", e);
            throw new TesseractException();     }

TEST CASE:

@Test
    public void testDoOcr() throws IOException {
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Accept", MediaType.APPLICATION_JSON_VALUE);
        headers.put("Content-Type", MediaType.APPLICATION_JSON_VALUE);
        Image image = new Image();
InputStream inputStream=lassLoader.getSystemResourceAsStream("eurotext.png");
        image.setUserId("arun0009");
        image.setExtension(".png");
       image.setImage(Base64.encodeBase64(IOUtils.toByteArray(inputStream)));
String response=given().contentType("application/json").headers(headers) .body(image).when().post("http://localhost:8080/ocr/v1/upload").then()
.statusCode(200).extract().response().body().asString();        System.out.println(response);  }

JSON:

    { "image": {  
               "userId": "arun0009", 
                "extension": ".png",    
                "text": "iVBORw0KGgoA"  
   }

JSON parse error:

Cannot deserialize instance of byte[] out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of byte[] out of START_OBJECT token\n at [Source: (PushbackInputStream);

1 Answers1

0

The concrete layout of the request json depends on the configuration of the JAX-RS framework, but by default it is a plain JSON object without a wrapper. In your case, I would send this JSON:

{
   "userId": "arun0009", 
   "extension": ".png",    
   "text": null,
   "image": "BASE_64_ENCODED_IMAGE"
}

The java side already "knows" what object to expect, so the wrapper you added is not necessary and (as you noticed) not valid to add.

  • I was applied your suggestion but the same type of " internal server error" and "Can not deserialize" error repeated again. I was applied the another suggestion from the below url regarding this error also but that's won't works. I thought there was issue about json format ca't recognized the encode details of image. Anyway Thanks. [https://stackoverflow.com/questions/16467035/spring-mvc-doesnt-deserialize-json-request-body/16467400#16467400] – Vijay Alangaram Samran Sep 16 '19 at 13:21