I’m trying make a POST request using K6. I’m fetching the request body from a APIs.json
file in my script.js
. When I try to use the request body in script.js
file it now consists of escape characters. How do I get rid of them? I tried JSON.parse
and JSON.stringify
, but did not work.
APIS.json:
{
"apiRequests":
[
{
"requestVerb" : "POST",
"requestHeaders" : [{"Content-Type":"application/json","Authorization":"Bearer $ACCESS_TOKEN"}],
"requestEndpoint" : "/individual",
"apiName": "management-4",
"target": "target1",
"responseVariables": ["id","name"],
"responseHeaders": [""],
"requestBody": {"title":"Mr","givenName":"Rohit","familyName":"Reddy"}
}
]
}
script.js
// To perform testing
import http from 'k6/http'
// To import API requests file
// import { open } from 'k6/fs';
const host = __ENV.HOST;
console.log(`Testing host = ${host}`);
// Ignore certificates
export const options = {
insecureSkipTLSVerify: true
};
// GET API Requests
let apiRequests = JSON.parse(open('./APIs.json'));
console.log(`log = ${apiRequests}`);
apiRequests = JSON.stringify(apiRequests);
console.log(`Stringify = ${apiRequests}`);
// K6 Main function
export default function() {
let httpVerb = "get";
http[httpVerb](`https://${host}/health/v1/health`);
}
Output:
time="2024-11-11T08:09:31Z" level=info msg="log = [object Object]" source=console
time="2024-11-11T08:09:31Z" level=info msg="Stringify = {"apiRequests":[{"requestVerb":"POST","requestHeaders":[{"Content-Type":"application/json","Authorization":"***"}],"requestEndpoint":"/individual","apiName":"management-4","target":"target1","responseVariables":["id","name"],"responseHeaders":[""],"requestBody":{"title":"Mr","givenName":"Rohit","familyName":"Reddy"}}]}" source=console
Expected output:
time="2024-11-11T08:09:31Z" level=info msg="log = [object Object]" source=console
time="2024-11-11T08:09:31Z" level=info msg="Stringify = {"apiRequests":[{"requestVerb":"POST","requestHeaders":[{"Content-Type":"application/json","Authorization":"***"}],"requestEndpoint":"/individual","apiName":"management-4","target":"target1","responseVariables":["id","name"],"responseHeaders":[""],"requestBody":{"title":"Mr","givenName":"Rohit","familyName":"Reddy"}}]}" source=console