4

I have a question for using Apach JMeter.

Our project, Android apps post json data with "Gzip Compression" to API server. The Android apps using "Apache HttpClient" and it's "GzipCompressingEntity" class.

For performance testing of API server, I tryed to recording the request by JMeter's Proxy (="HTTP(S) Test Script Recorder"). But the recorded request body was empty.

What I want to do is to send "Gziped HTTP request data" from Apache JMeter to server. Is there any way for that?

Following is sample of our Android app's request header.

POST /api/test HTTP/1.1
Content-Type: application/json
Transfer-Encoding: chunked
Content-Encoding: gzip
Host: 192.168.11.11:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.5)
Accept-Encoding: gzip,deflate

RequestBody is Gziped binary data.

What I had done is

  1. Run the "HTTP(S) Test Script Recorder(Proxy Server)" on JMeter.
  2. Set http proxy of Android to use that proxy server.
  3. Execute Android test code of HTTP Client (the post data is compressed by Gzip).

then, that test code finished with failure. (no response from server)

If access directly (without JMeter's Proxy Server), that test succeeded. Can I send the compressed request data from JMeter just like this?

tf-oikawa
  • 43
  • 1
  • 4
  • Welcome to Stackoverflow. Please first, use the serach function before asking because there may already be other questions with answers to the same topic, [like this one](http://stackoverflow.com/questions/2900504/how-to-get-jmeter-to-request-gzipped-content). If you find questions but their answers aren't sufficent or the questions itself don't match, add them to your post, explaining they're not related. Please also add details what you've done so far. – try-catch-finally Nov 15 '15 at 10:27
  • Thanks for your advices. Sorry my description was not enough. I had checked that question, but I thought it doesn't match with my problem. I can receive the response **from server** with Gzip compressed. However, I could not send the Gziped **"Request Data"**. I want to compress the "request data" from JMeter. (Direction is **"Client => Server"**) – tf-oikawa Nov 15 '15 at 12:24
  • Please update your question with this information instead of in the comments. – RaGe Nov 16 '15 at 15:38
  • Does your android application use HTTPS? – RaGe Nov 16 '15 at 15:40
  • No. I didn't use SSL, when I record the request data. – tf-oikawa Nov 16 '15 at 16:58

2 Answers2

5
  1. Add HTTP Header Manager to your test plan and add at least the following headers:

    • Content-Type: application/json
    • Content-Encoding: gzip
  2. Add Beanshell PreProcessor as a child of the request which you need to encode and add the following code to it's "Script" area:

    import org.apache.commons.io.IOUtils;
    import java.util.zip.GZIPOutputStream;
    
    
    String bodyString = sampler.getArguments().getArgument(0).getValue();
    byte [] requestBody = bodyString.getBytes();
    
    ByteArrayOutputStream out = new ByteArrayOutputStream(requestBody.length);
    GZIPOutputStream gzip = new GZIPOutputStream(out);
    gzip.write(requestBody);
    gzip.close();
    
    sampler.getArguments().getArgument(0).setValue(out.toString(0));
    

    It will get your request body, compress it and substitute on the fly so the request will be gzipped.

Emmanuel Courreges
  • 708
  • 2
  • 6
  • 13
Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks! I succeeded to make the Gziped request by **Beanshell PreProcessor**. I really appriciate your answer. – tf-oikawa Nov 17 '15 at 15:42
  • Note that if your HTTP Request sampler has the "Content Encoding" field filled out, that will mess up the compression. You need to set that field to blank. If you have a ton of requests with it set, you can easily open the JMX file in a text editor and do a search-and-replace to remove it. – Brian Reischl Apr 13 '16 at 21:09
  • to complete the very good remark of @BrianReischl, he's talking about the content encoding field in the HTTP Request sampler (right next to the URL to call), and NOT a Content-Encoding header that you may have added in a HTTP Header Manager. – Sacapuces Jun 23 '20 at 12:42
0

Encoding is wrong, you need to save it in body like this

sampler.getArguments().getArgument(0).setValue(new String(compressedBody, 0));
MichiD
  • 1