使用RestClient或Postman发送gzip数据

4
我该如何使用restclient或postman发送gzip数据作为请求体的一部分?我使用了apache restclient 3.2.2,但无法得到响应。我已附上参考图片。
基本上,我有一个xml文件,想要先将其转换为gzip,然后作为请求体的一部分发送。
为了转换为GZip,我使用在线工具先将我的xml文件转换为gzip,并将转换后的gzip文件包含在我的restclient请求体中。
我得到了以下java代码,并且使用代码可以正常获取响应。但是无法在restclient工具中运行!
    URL url = new URL(String.format("%s?tid=%d",
                sessionInfo._getMessagesUrl, tpegId));
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type",
                "application/octet-stream");
        connection.setUseCaches(false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        // Convert XML to initial binary payload.
        byte[] bytesToSend = getMessagesRequestXml.getBytes("UTF-8");
        String fileName = "C:\\\\Sanjay\\\\Work\\\\17MM\\\\MM17_body.txt";
        if (this._outputFilename != null) {
            System.out.println(String.format("\nWriting body file '%s'", fileName));
            FileOutputStream s = new FileOutputStream(fileName);
            s.write(bytesToSend);
            s.close();
        }
        dumpBinary("Original GetMessages request", bytesToSend);

        // Optionaly compress
        if (this._shouldCompress) {
            byte[] gzippedBytes = compressToGzip(bytesToSend);
            bytesToSend = gzippedBytes;
            dumpBinary("Compressed GetMessages request", bytesToSend);
        }

        // Optionally encrypt
        if (sessionInfo._encryptionKey != null) {
            byte[] encryptedBytes = encrypt(bytesToSend, sessionInfo._encryptionKey);
            bytesToSend = encryptedBytes;
            dumpBinary("Encrypted GetMessages request", bytesToSend);
        }

        // Send request
        System.out.println(String.format("Sending GetMessages Request: %s\n", url.toString()));         
        DataOutputStream os = new DataOutputStream(
                connection.getOutputStream());
        os.write(bytesToSend, 0, bytesToSend.length);
        os.flush();
        os.close();
1个回答

0
我发现您需要将gzip作为filebody传递给restclient。这解决了我的问题。

我有一个类似的问题,但是使用GET方法。我正在返回一个经过gzip压缩的JSON响应给某个GET请求。我的浏览器会自动解压它。但是Postman返回了一些无法理解的东西。你有什么想法我该怎么做吗? - riroo
这里也有同样的问题,有没有解决办法可以在Postman中对数据进行解码? - Derek Hannah
如何在REST高级客户端中将gzip作为文件主体传递?谢谢。 - Fernando Pie

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接