1

When I use bean shell script to preload the file to a variable and then set it in parameter of HTTP request sampler to upload file, from the wireshark packets I find that the content-length is not right. Also the content-type is set to text/plain which cause problem. How can I change it?

Below are some pictures to show the jemter setup and wireshrak snoops.

The wireshark snoop when the content of the file set as the value of the parameter of the Http request sampler The jmeter test element setup showing that the the content of the file is set in the http request parameter

Ripon Al Wasim
  • 36,924
  • 42
  • 155
  • 176
user1532146
  • 184
  • 2
  • 14

1 Answers1

1

You're using wrong configuration:

  1. Check Use multipart/form-data for POST box
  2. Remove your file from Parameters area
  3. Add it to Send Files With the Request area like:

    • File Path: ${__property(FileData,,)}
    • Parameter Name: File
    • MIME Type: text/plain

      Example upload configuration

See Performance testing: Upload and Download Scenarios with Apache JMeter guide for more information on simulating file upload and download events in your JMeter script.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • The reason I need to preload the file content to a string is that I don't want to load file from the disk in every upload request because it takes time to load 2M file. So I load the file in my bean shell script in my setup thread group and try to use it in send parameters as I think 'the send files' section only allow me to provide file path. Any idea about how to implement file preloading? – user1532146 Apr 12 '16 at 14:06
  • My bean shell script looks this: import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; String filePath = vars.get("uploadFilePath"); print("filePath:" + filePath); FileInputStream in = new FileInputStream(filePath); 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[] fileData = bos.toByteArray(); print("length of file:" + fileData.length); bos.close(); props.put("fileData", new String(fileData)); – user1532146 Apr 12 '16 at 15:06
  • Following this link: http://stackoverflow.com/questions/23101904/how-to-send-byte-array-in-http-request-in-jmeter, is it possible to implement upload preloded file using http raw request? – user1532146 Apr 12 '16 at 15:08
  • I didn't ask "why do you need this", I pointed to wrong configuration of your HTTP Request sampler – Dmitri T Apr 12 '16 at 16:19
  • Maybe I misunderstood something in the above link, it says " 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. – user1532146 Apr 12 '16 at 16:34