user842122
user842122

Reputation: 295

JMeter - Read Image from Data file for PUT Request

I have a csv data as

FirstName,MiddleName,LastName,ImageLocation
Jack|Michel|Rechards|C:\Image\picture.jpg
Tom|Peter|Kim|C:\Image\picture123.jpg

I'm trying to configure Jmeter to read above data file and pass image as form-data to REST API PUT resource as form-data. API accepts image as ByteBuffer.

In JMeter, multipart/form-data upload is only available for POST but not for PUT resource.

For Image, I have written a code in BeanShell PreProcessor that puts byte[] in a variable

String imageLoc = vars.get("ImageLocation");
File file = new File(imageLoc);
byte[] buffer = new byte[(int) file.length()];
InputStream ios = null;
    try {
        ios = new FileInputStream(file);
        if (ios.read(buffer) == -1) {
            throw new IOException(
                    "EOF reached while trying to read the whole file");
        }
    } finally {
        try {
            if (ios != null)
                ios.close();
        } catch (IOException e) {
        }
    }
vars.put("imageData", new String(buffer));

and the variable imageData is passed in HTTP request body data as

------=_parttest
Content-Type: image/jpeg; name=test.jpeg
Content-Transfer-Encoding: binary
Content-Disposition: form-data; name="Picture"; filename="test.jpeg"
${imageData}
------=_parttest--

For some reason, images are not rendered correctly for requests from Jmeter. If I make similar postman PUT request to API to save image and make another GET request to read image, it's read successfully.

Either I have not configured my test correctly(beanShell code issue or HTTP Request body data issue) or there is a better way to configure this test for reading images from path in data file and pass image as form-data to API PUT resource.

Looking forward to experts advice.

Upvotes: 0

Views: 1710

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

It is actually possible to perform a multipart PUT request using JMeter, you will need to:

  1. Add a HTTP Header Manager and configure it to send Content-Type header with the value of:

    multipart/related; boundary=parttest
    
  2. Construct your request body in the HTTP Request using the same boundary value as in the Content-Type header

See Testing REST API File Uploads in JMeter for example test plan which updates a document in Google Drive using PUT request, you can use it as a reference.

Also I would rather suggest using __FileToString() function in order to read the image file contents or if you prefer scripting - go for JSR223 PreProcessor and Groovy language instead.

Upvotes: 1

Related Questions