Download and upload image files

I have to perform a basic test where I download file from one url and upload the file to another url.

I tried the following approach:

  1. Downloaded the file and stored it in a variable like below:
    fileDownloaded = res.body;
  2. Uploaded the file with the response data as:
    file: http.file(fileDownloaded, “default_report_image.png”, “b”)

I am getting some file format issue when I open the file in my destination.

Hi @masti13harsh,

set responseType to binary in your request, so that k6 returns a byte array in response:

The response type is binary.
While uploading the file again, the file gets corrupted

In order to test your case, I’ve made a simple app in Flask with Python, to act as a webserver for download and upload:

from flask import Flask, render_template, request, send_from_directory
from werkzeug import secure_filename


app = Flask(__name__, static_url_path='')


@app.route('/download/<filename>')
def download_file(filename):
    return send_from_directory('./', filename)


@app.route('/upload', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        fp = request.files['file']
        fp.save(secure_filename(fp.filename))
        return 'file uploaded successfully'


if __name__ == '__main__':
    app.run(debug=True)

Next, I have installed and run the above file with python (assuming you already have the Python interpreter and pip package manager installed):

$ pip install flask
$ python app.py

Then, I tried downloading a file, named test.pdf, and uploading it back, to be named test1.pdf on the server:

import http from 'k6/http';

export default function () {
    let params = {
        responseType: 'binary'
    };
    let res = http.get("http://127.0.0.1:5000/download/test.pdf", params);

    let data = {
        file: http.file(res.body, "test1.pdf")
    };

    res = http.post("http://127.0.0.1:5000/upload", data);
    console.log(res.body);
}

And it worked like a charm:

$ k6 run -i 1 --vus 1 --duration 5s test.js

          /\      |‾‾|  /‾‾/  /‾/   
     /\  /  \     |  |_/  /  / /    
    /  \/    \    |      |  /  ‾‾\  
   /          \   |  |‾\  \ | (_) | 
  / __________ \  |__|  \__\ \___/ .io

  execution: local
     script: test.js
     output: -

  scenarios: (100.00%) 1 executors, 1 max VUs, 35s max duration (incl. graceful stop):
           * default: 1 iterations shared among 1 VUs (maxDuration: 5s, gracefulStop: 30s)

INFO[0001] file uploaded successfully                   

running (00.5s), 0/1 VUs, 1 complete and 0 interrupted iterations
default ✓ [======================================] 1 VUs  0.5s/5s  1/1 shared iters


    data_received..............: 11 MB 20 MB/s
    data_sent..................: 11 MB 20 MB/s
    http_req_blocked...........: avg=343.67µs min=322.06µs med=343.67µs max=365.29µs p(90)=360.97µs p(95)=363.13µs
    http_req_connecting........: avg=198.76µs min=198.51µs med=198.76µs max=199µs    p(90)=198.95µs p(95)=198.97µs
    http_req_duration..........: avg=233.5ms  min=41.29ms  med=233.5ms  max=425.72ms p(90)=387.28ms p(95)=406.5ms 
    http_req_receiving.........: avg=19.68ms  min=243.39µs med=19.68ms  max=39.12ms  p(90)=35.23ms  p(95)=37.17ms 
    http_req_sending...........: avg=76.26ms  min=113.16µs med=76.26ms  max=152.41ms p(90)=137.18ms p(95)=144.8ms 
    http_req_tls_handshaking...: avg=0s       min=0s       med=0s       max=0s       p(90)=0s       p(95)=0s      
    http_req_waiting...........: avg=137.55ms min=2.05ms   med=137.55ms max=273.06ms p(90)=245.96ms p(95)=259.51ms
    http_reqs..................: 2     3.79285/s
    iteration_duration.........: avg=474.47ms min=474.47ms med=474.47ms max=474.47ms p(90)=474.47ms p(95)=474.47ms
    iterations.................: 1     1.896425/s

I hope this helps you achieve your goal.

2 Likes