How to add a file name as data for get or post request

Hi,

I am working on performance testing of spring batch job api’s where I need to just give the name of the file for each job and thet input file will be read by api automatically and will be processed. I do not need to open the file or do anything with that so the k6 feature of open file cannot be used. Can someone please help me which function can I use just to give the name of the file for get, post or put request. Attaching the screenshot for reference.

Hi @nitisha

Welcome to the community forum :wave:

I understand you have seen the multipart request example from the docs. Which uses the FormData polyfill for k6 to upload one (or more) files. Hence the open()method, to send the file from the load generator instance to the SUT (System Under Test).

From the screenshot I imagine you are using AirFlow. And if I understand correctly, you could use the http.request method to send the field names and values that the API endpoint expects. If the API does not expect a file upload, there is no need to send a multipart request via FormData().

Similar to:

import http from 'k6/http';

const url = 'https://httpbin.test.k6.io/post';

export default function () {
  const data = { fileName: 'the-file-name', jobName: 'TECJDB01',  };

  // E.g. using a JSON string as body if the API expects JSON
  let res = http.request('POST', url, JSON.stringify(data), {
    headers: { 'Content-Type': 'application/json' },
  });
}

If this fails, can you share a bit more details of the errors you get? Running the tests with --http-debug="full" can help understand what is failing. Especially if you can compare the requests that are already working with other tools (Postman, curl, etc.).

I hope this helps.

Cheers!

Thank you so much for the quick response. Actually this would work for post request but in my case I have Get request and for that how will I use the data part because in the documentation for get only the optional params we can add.

Thank you in advance.

Hi @nitisha

For get requests, you usually would pass the parameters in the URL. I’m not sure if this will be your case, and --http-debug="full" with your API can help. Would this work for you?

import { URL } from 'https://jslib.k6.io/url/1.0.0/index.js';
import http from 'k6/http';

export default function () {
  const url = new URL('https://k6.io');

  url.searchParams.append('fileName', 'the-file-name');
  url.searchParams.append('jobName', 'TECJDB01');

  const res = http.get(url.toString());
  // https://k6.io?fileName=the-file-name&jobName=TECJDB01
}

Cheers!

That is the case mostly with the api’s but in my case the input file is first picked up from the gcs bucket and then the job runs which means I cannot attach the file to the url. I just need to give the file name before hitting the request so that the correct file is picked up. Is there a way I can give that in Get request?

Hi,
Just to reiterate on my question above I have a scenario where irrespective of the type of http request the file needs to be read so in the case of Post request we can add it as data similary is there any way we can just add some function just to make that file be read before hitting the endpoint for the Get request, because mostly all the request which I have in 1st spring batch are get request but the file name I still need to provide.

Thanks in advance.

Hi @nitisha

What do you mean by:

I just need to give the file name before hitting the request

I’m afraid I don’t understand how you need to attach the file to the url in the get requests yet. Can you share a similar curl command you would use to invoke that endpoint for both post and get?

Thanks!

Hi @eyeveebe

In my case everytime I trigger an endpoint before that I need to give the deposit file name which will be automatically processed in the gcs bucket. So, I don’t need to add that file name as data, I just want that before the endpoint is hit the file should be read so that the job can be run.

In the below screenshot you can see there is a deposit file " TECJDCCV manual__2023-02-08T04:17:09.253532" which is being processed first before the get url “https://gmasqa.gpapiservices.com/card-service-batch/invokeTecjdccvJob” is triggered.

So, I want to just give this file name before I hit the get url.

Thanks!