How to Send Byte Array in http request in Jmeter

不问归期 提交于 2019-12-20 02:15:30

问题


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

Can anyone help me how it would be possible through jmeter.

Your help would really be appreciated.


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/23101904/how-to-send-byte-array-in-http-request-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!