0

I reference this python send files to send file. And this is my python frontend code. (I have a backend to receive the files["img"])

import requests

host = "http://example.org:5000"

with open("./test.png", "rb") as f:
    r = requests.get(host, files={"img": f})
    print(r.text)

So the structure would like this

{
  "origin": ...,
  "files": {
    "img": "<censored...binary...data>"
  },
  "form": {},
  "url": ...,
  "args": {},
  "headers": {
    ...
  },
  "data": ...
}

And I want to use k6 to testing it. I've read k6 file and write the script like this.

import http from "k6/http";

const img = open("./test.png", "b");

export default function() {
    const params = {files: {"img": img}};
    http.get("http://example.org:5000", params);
}

It seems that I did wrong. The request always failed. How can I fix that?

eeeXun
  • 1
  • 1

1 Answers1

1

You are calling http.get which will send a GET request. To upload a file you need a POST request. You also need to wrap it in http.file. See Multipart request in the k6 documentation:

import http from 'k6/http';

const binFile = open('/path/to/file.bin', 'b');

export default function () {
  const data = {
    file: http.file(binFile, 'test.bin'),
  };

  const res = http.post('https://example.com/upload', data);
}

With your code:

import http from "k6/http";

const img = open("./test.png", "b");

export default function() {
    const params = {img: http.file(img, 'test.png')};
    http.post("http://example.org:5000", params);
}
knittl
  • 246,190
  • 53
  • 318
  • 364