0
votes

I'm unable to send both, JSON body data and a video/image file together in one request on JMeter. It gives a 401 unauthorized, even though I have added the Authorization header in the HTTP Header Manager. The same request with headers works fine on Postman.

I've already referred to this guide here with no luck.

https://www.blazemeter.com/blog/testing-advanced-rest-api-file-uploads-jmeter/

Content-Type: multipart/form-data

{
    "email": "[email protected]",
    "password": "xyz",
    "file": ${__FileToString("/Downloads/SampleVideo_1280x720_1mb.mp4")}

}

SAMPLER RESULT:

Thread Name: Thread Group 1-1
Sample Start: 2019-09-30 14:26:25 IST
Load time: 2922
Connect Time: 838
Latency: 2922
Size in bytes: 202
Sent bytes:1056397
Headers size in bytes: 202
Body size in bytes: 0
Sample Count: 1
Error Count: 1
Data type ("text"|"bin"|""): 
Response code: 401
Response message: Unauthorized


HTTPSampleResult fields:
ContentType: 
DataEncoding: null

REQUEST:

POST data:
--o3F8APyqP080W3wk0N_-96jzl11Bfsa
Content-Disposition: form-data; name="file"; filename="SampleVideo_1280x720_1mb.mp4"
Content-Type: video/mp4
Content-Transfer-Encoding: binary

<actual file content, not shown here>
--o3F8APyqP080W3wk0N_-96jzl11Bfsa--


[no cookies]
2

2 Answers

0
votes

JMeter should be doing what real browser (or other application using your API endpoint) is doing.

If the browser (or the application) sends 2 requests - JMeter must be sending 2 requests as well. If the browser (or the application) sends 1 request - JMeter must be sending 1 request. In case of 1 request my expectation is that you should be sending application/json as the value of the Content-Type header like it's done in the guide you're referring.

--boundary
Content-Type: application/json; charset=UTF-8



JSON Metadata
--boundary
Content-Type: file MIME type



File content
--boundary--

The answer is quite simple: just record the request using JMeter's HTTP(S) Test Script Recorder and JMeter will come up with proper configuration of the HTTP Request sampler(s), HTTP Header Manager, etc. All you will need to do is to implement parameterization and correlation so the script could be replayed successfully.

One important bit: during the recording you need to have SampleVideo_1280x720_1mb.mp4 file in JMeter's "bin" folder as modern browsers don't return the full path hence JMeter will be able to properly capture the file upload request only if the file lives in its "bin" folder. More information: Recording File Uploads with JMeter

0
votes

To post json body and attachment you can add your "email" and "password" in Parameters tab of JMeter HTTPS request and file in Files Upload tab.