1

An API is sending a large amount of data in the body in form of GZIP, I need to create rest API to decrypt and save it in the database, but I am not able to decrypt the data.

`@GetMapping
    public void hello() throws IOException {
        String payload = "{\n" +
                "    \"name1\": \"shrikant\",\n" +
                "    \"date\": \"Fri Apr 05 15:48:59 IST 2019\"\n" +
                "}";
        String urlStr = "http://localhost:8080/hello";
        URL url = new URL(urlStr);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(60000);
        conn.setConnectTimeout(60000);
        conn.setRequestMethod("POST");
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestProperty("Accept", "application/json");
        conn.setRequestProperty("Content-Type", "application/json");
        conn.addRequestProperty("Content-Encoding", "gzip");
        OutputStream os = conn.getOutputStream();
        GZIPOutputStream gos = new GZIPOutputStream(os);
        gos.write(payload.getBytes(StandardCharsets.UTF_8));
        System.out.println("payload " + 
        Arrays.toString(payload.getBytes(StandardCharsets.UTF_8)));
        os.close();
        int responseCode = conn.getResponseCode();
    System.out.println("\nSending 'POST' request to URL : " + url);
    System.out.println("Response Code : " + responseCode);
    }`

API to receive the data.

     @PostMapping("hello")
    public byte[] hello1(HttpServletRequest request) throws IOException {
        System.out.println("hi");
        ByteArrayInputStream bis = new ByteArrayInputStream();
        GZIPInputStream gis = new GZIPInputStream(bis);
        BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
        StringBuilder sb = new StringBuilder();
        String line;
        while((line = br.readLine()) != null) {
            sb.append(line);
        }
    }

but not able to decrypt data.

  • how to decrypt request.
shrikant.sharma
  • 203
  • 1
  • 17
  • 37
  • Possible duplicate of [Exception: Unexpected end of ZLIB input stream](https://stackoverflow.com/questions/24531089/exception-unexpected-end-of-zlib-input-stream) – Mebin Joe Apr 08 '19 at 11:12

1 Answers1

2

On the client side you should close GZIPOutputStream before closing OutputStream.

    gos.close();
    os.close();

On the server side you should use InputStream from request

    ServletInputStream inputStream = request.getInputStream();
    GZIPInputStream gis = new GZIPInputStream(inputStream);
    BufferedReader br = new BufferedReader(new InputStreamReader(gis, "UTF-8"));
    StringBuilder sb = new StringBuilder();
    String line;
    while((line = br.readLine()) != null) {
        sb.append(line);
    }

    System.out.println(sb.toString());

For streams better use try-with-resources blok you won't have to remember about closing streams.

    try (OutputStream os = conn.getOutputStream()) {
        try (GZIPOutputStream gos = new GZIPOutputStream(os)) {
            gos.write(payload.getBytes(StandardCharsets.UTF_8));
        }
    }
Daniel Husak
  • 111
  • 2