3

I`m using JMeter for load testing where I have to call the upload Image API through an HTTP request, and to achieve this I have to convert an image into a compressed byte array to send it out as post data through an HTTP request.

Can anyone show me how it would be possible through JMeter?

Your help would really be appreciated.

David Klempfner
  • 8,700
  • 20
  • 73
  • 153
Saurabh Gupta
  • 107
  • 1
  • 3
  • 10

2 Answers2

5

There are several options on how you can proceed:

  1. You can use HTTP Raw Request Sampler (available through JMeter Plugins site) which gives you full control on what, how and where you send.

  2. Have you tried enabling Use multipart/form-data for POST for HTTP Request Sampler? This is how files should be uploaded as per RFC-1867.

  3. If your use case is specific and none of the above is applicable, you can always use JMeter Scripting extensions. For example if you add a Beanshell Pre Processor to your HTTP Request which performs file upload with something like:

    FileInputStream in = new FileInputStream("/home/glinius/401.png");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    for (int i; (i = in.read(buffer)) != -1; ) {
        bos.write(buffer, 0, i);
    }
    in.close();
    byte[] imageData = bos.toByteArray();
    bos.close();
    vars.put("imageData", new String(imageData));
    

You'll be able to add ${imageData} parameter in your POST request.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Thanks for detailed Information. But I want to know how could I use HTTP Raw Request Sampler in this case where I have to convert an image into gzip compressed byte array. – Saurabh Gupta Apr 16 '14 at 09:13
  • Also, I followed your last point and following is the snippet of the code : – Saurabh Gupta Apr 16 '14 at 09:48
  • import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; FileInputStream in = new FileInputStream("C:\\Users\\i\\Downloads\\Screen 2.jpg"); ByteArrayOutputStream bos = new ByteArrayOutputStream(); int size = (int) file.length(); byte[] buffer = new byte[size]; for (int i; (i = in.read(buffer)) != -1; ) { bos.write(buffer, 0, i); } in.close(); byte[] imageData = bos.toByteArray(); bos.close(); vars.put("imageData", new String(imageData)); – Saurabh Gupta Apr 16 '14 at 09:51
  • Here , I`m getting "Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import . . . '' : Typed variable declaration : Attempt to resolve method: length() on undefined variable or class name: file". Could you please let me know what I`m doing wrong here – Saurabh Gupta Apr 16 '14 at 09:52
  • Your `file` variable is not defined, please amend 2 first lines of code to be: File file = new File("C:\\Users\\i\\Downloads\\Screen 2.jpg"); FileInputStream in = new FileInputStream(file); – Dmitri T Apr 16 '14 at 12:22
2

Yes, I follow this method "add a Beanshell Pre Processor to your HTTP Request", and successful.

For my case, I also add a "HTTP Header Manager", specify: "Content-Encoding:gzip", "Content-Type:"application/x-www-form-urlencoded", "Accept:/". And, set String encoding by: vars.put("binaryData", new String(binThrift, "ISO-8859-1"));

HTTP Header Manager

Beanshell Pre Processor

HTTP Request

Real Request

  • Thank you so much! I spent hours trying to figure out why my request failed and your way of encoding the byte array is precisely the correct one. – Marduk Jan 27 '21 at 18:41