coolboywcm commented on issue #9961: URL: https://github.com/apache/apisix/issues/9961#issuecomment-1665119637
> "uri": "http://xxx.xx.xx.xx:80/apisix/addLog" controller method addLog is as follows ``` import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; public void addLog() throws IOException { String encoding = this.getRequest().getCharacterEncoding(); System.out.println("1encoding=========================================>" + encoding); if (encoding == null) { encoding = StandardCharsets.UTF_8.name(); } System.out.println("2encoding=========================================>" + encoding); StringBuilder stringBuilder = new StringBuilder(); BufferedReader reader = new BufferedReader(new InputStreamReader(this.getRequest().getInputStream(), encoding)); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } System.out.println("00000000000000stringBuilder.toString()============================================>" + stringBuilder.toString()); String jsonString = stringBuilder.toString(); JSONObject logJson = JSONArray.parseArray(jsonString).getJSONObject(0); jsonString = logJson.toJSONString(); System.out.println("3333333333333jsonString============================================>" + jsonString); String resp_body = logJson.getJSONObject("response").getString("body"); System.out.println("addLog=================================>111resp_body:" + resp_body); resp_body = decompress(resp_body.getBytes(encoding); System.out.println("addLog=================================>222resp_body:" + resp_body); } ``` method decompress is as follows ``` import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; public static String decompress(byte[] compressedData) throws IOException { try (ByteArrayInputStream bis = new ByteArrayInputStream(compressedData); ByteArrayOutputStream bos = new ByteArrayOutputStream(); GZIPInputStream gzipIS = new GZIPInputStream(bis)) { byte[] buffer = new byte[1024]; int len; while ((len = gzipIS.read(buffer)) > 0) { bos.write(buffer, 0, len); } return bos.toString("UTF-8"); } } ``` console print result: ``` 1encoding=========================================>UTF-8 2encoding=========================================>UTF-8 00000000000000stringBuilder.toString()============================================>[{"response":{"status":200,"headers":{"connection":"close","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","content-encoding":"gzip","date":"Fri, 04 Aug 2023 06:52:28 GMT","access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","access-control-allow-methods":"GET, POST","content-length":"37"},"body":"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000","size":377}}] 3333333333333jsonString============================================>{"response":{"status":200,"headers":{"connection":"close","access-control-allow-origin":"*","server":"APISIX\/3.1.0","content-type":"application\/json;charset=UTF-8","content-encoding":"gzip","date":"Fri, 04 Aug 2023 06:52:28 GMT","access-control-allow-headers":"Content-Type,X-Requested-With,withCredentials","access-control-allow-methods":"GET, POST","content-length":"37"},"body":"\u001f�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000","size":377}} addLog=================================>111resp_body:\u001F�\b\u0000\u0000\u0000\u0000\u0000\u0000\u0000�V�N�4T�R242626Q�\u0005\u0000��_�\u0011\u0000\u0000\u0000 java.util.zip.ZipException: Not in GZIP format at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:165) at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:79) at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:91) at com.ApiSixController.decompress(ApiSixController.java:332) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
