1
votes

I am trying to perform a file upload operation using jmeter for an API POST method. In Jmeter it requests results in 403 but in webpage it is 200.

Referred as many online resources and given multiple attempts i am not able to crack the solution.

How to handle the below form data in jmeter 4.0, As you could notice, there are 2 things, files and filedetails. How to handle this ?

I have also included the header value. Please look into it.

Below is the form data of POST METHOD:

------WebKitFormBoundarylyJiQdD1alYKbAKW Content-Disposition: form-data; name="files"; filename="Sample_Report.pdf" Content-Type: application/pdf

------WebKitFormBoundarylyJiQdD1alYKbAKW Content-Disposition: form-data; name="filedetails"

{"serviceRequestId":"RandomNumber","filedetails":[{"fileName":"Sample_Report.pdf","fileUniqueName":"Randomnumber-a575d680-ceb0-11e9-a25d-2590f89b0ed0.pdf","fileType":"application/pdf","fileSize":1117730,"status":"New"}]} ------WebKitFormBoundarylyJiQdD1alYKbAKW--

Request Headers:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundarylyJiQdD1alYKbAKW

I should be able to upload a single / multiple file through the JMeter.

2

2 Answers

1
votes

To upload a file using JMeter, try following approach:

  1. Use HTTP Request Sampler and add all the necessary details like Server, Port, Path, Method etc.
  2. In File Upload section of HTTP Sampler include following

    File Path = Sample_Report.pdf
    Parameter Name = files
    MIME Type = multipart/form-data [if this does not work use application/pdf]
    
  3. In Parameters section of HTTP Sampler include following

    Name = filedetails
    Value = {"serviceRequestId":"RandomNumber","filedetails":[{"fileName":"Sample_Report.pdf","fileUniqueName":"Randomnumber-a575d680-ceb0-11e9-a25d-2590f89b0ed0.pdf","fileType":"application/pdf","fileSize":1117730,"status":"New"}]}
    
  4. Make sure to check Use multipart/form-data

  5. Add header whichever is required in Header Manager [initially dont include this, if it does not work with above settings, then only include Header Manager]

enter image description here enter image description here enter image description here

1
votes

The easiest way of configuring JMeter to simulate file upload is just recording it using HTTP(S) Test Script Recorder

  1. Configure JMeter for Recording. The easiest way is using JMeter Templates feature

    • From JMeter's main menu choose "File -> Templates - Recording" and click "Create"
    • Expand HTTP(S) Test Script Recorder and click "Start"
  2. Copy the file(s) you will be uploading to "bin" folder of your JMeter installation

  3. Perform the upload in the browser
  4. JMeter will generate the relevant HTTP Request sampler under the Recording Controller

For more complex scenarios you might require building your request manually, refer Testing REST API File Uploads in JMeter article for more details if needed.