Form-data content type to be multipart but the data is coming as octalstram

My code

import http from "k6/http";
import { check } from "k6";
import { FormData } from "https://jslib.k6.io/formdata/0.0.1/index.js";

const img1 = open("/C:/Users/M1059024/Downloads/test_photo.png", "b");

export default function () {
  const requestPayload = new FormData();  
  //requestPayload.append('data', http.file(img1, '2.png', 'image/png'));
  requestPayload.append('images', http.file(img1, 'image1.png'));
  requestPayload.append("env", "local");
  requestPayload.append("brandcode", "CBR");
  requestPayload.append("agentmasterid", "Q00200000FDFAr8QCHj9H2tck8J5eXHfCZHRJC7C");
  requestPayload.append("guid", "488EF450-AAB2-46F3-A81A-73758A59ED5B");
  requestPayload.append("cbagentid", "");
  requestPayload.append("loadflag", "False");
  console.log("test");
  console.log(requestPayload.body());
      
  const requestHeader = {
    apiKey: "2QucUpePK1nnirL6LhjBc4QAhxwXSutm",
    Authorization: "Bearer <redacted>",
    "Content-Type": "multipart/form-data; boundary=" + requestPayload.boundary,
  };
      
  const res = http.post(
    'https://lsn2ob7pb5.execute-api.us-west-2.amazonaws.com/dev-apm-photo/photo',
    requestPayload.body(),
    {
      headers: requestHeader,
    }
       
  );
}

I am expecting the form-data content type to be multipart but the data is coming as octalstram. Can any please help me fix this issue?

1 Like

Hi, welcome to the forum.

This is likely because of the way you’re appending string fields. This is not well documented at the moment (see Improve FormData documentation · Issue #225 · grafana/k6-docs · GitHub), but it should work if you append the string fields this way: requestPayload.append("env", {data: "local"});.

We plan to change the polyfill to also support this API (see Make plain text form fields easier to use with FormData · Issue #23 · grafana/jslib.k6.io · GitHub), but you can use this workaround in the meantime.

HI Imiric,

Thanks for the reply. I tried ur solution above and i am getting unsupported format > error.

INFO[0003] Response:
HTTP/2.0 415 Unsupported Media Typel
Content-Length: 17-
Access-Control-Allow-Credentials: true
Access-Control-Allow-Headers: *
Access-Control-Allow-Methods: OPTIONS,POST,GET,PUT
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers: *
Content-Type: application/json

Hello! We’ll need to see a dump of the request to diagnose why you’re receiving the response. It is basically saying the request is not in the expected format.