How to upload file

I have request in postman


now, i need to implement this request in k6.

{
    "requests": [
        {
            "name": "Upload file",
            "type": "POST",
            "url": "https://###########/###########/rs/dossier/###########/add",
            "header_value": ["Basic ##########","multipart/form-data"],
            "header_tag": ["Authorization","Content-Type"],
            "body": {
                "mode": "formdata",
                "formdata": [
                    {
                        "key": "content",
                        "type": "application/pdf",
                        "src": "DocToAddFilenet1.pdf"
                    },
                    {
                        "key": "document",
                        "type": "text/plain",
                        "data": "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <Document> <Code>010001</Code> <Type> <Code>1210</Code> <Name>Load Testing</Name> </Type> <Number>8884545</Number></Document>"
                    }
               ]
            },
            "expectedStatus": 200
        }
    ]
}

this is the json file that passes data to the main script.js file

let requests = {
        names:{
            method:GetType,
            url: urlTemplate,
            body:body_b,
            params:authTemplate
        }
    }

this is the request method in script.js file
I tried, but there are errors.
I’m just starting to study k6, help me to implement this request in k6.

Hi,

what errors are you getting? Also, which k6 version are you using (output of k6 version)?

This is possible with multipart requests. See the documentation.


k6 version is: v0.26.2 (dev build, go1.13.8, windows/amd64)

Any reason you’re using a dev build? You can download official releases from Releases · grafana/k6 · GitHub, and you might as well upgrade to v0.27.0. EDIT: Sorry, the dev version is a known issue with our Windows builds, so your version is fine.

Though the version is likely unrelated. You seem to be sending JSON instead of binary data. Please follow the example in the documentation.