如何将 json 文件的内容作为 url 参数传递
How to pass content of json file as url param
我正在尝试学习数据参数化。
我有一个包含 2 个条目的 json 文件:
[
{
"accountreference": "4157804914681"
},
{
"accountreference": "4157804925075"
}
]
我设法将它们打印到控制台中。但是,我无法进入 http.post.
这有什么问题吗:
const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';
有更好的传递方式吗?
import http from "k6/http";
import { check, sleep } from "k6";
import { SharedArray } from 'k6/data';
// Test setup
export let options = {
stages: [
{ duration: '3s', target: 1 },
{ duration: '3s', target: 0 }, // scale down. Recovery stage.
],
thresholds: {
// 90% of requests must finish within 400ms, 95% within 800, and 99.9% within 2s.
http_req_duration: ['p(90) < 400', 'p(95) < 800', 'p(99.9) < 2000'],
},
};
const data = new SharedArray('accountRef', function () {
// here you can open files, and then do additional processing or generate the array with data dynamically
const f = JSON.parse(open('./accountRef.json'));
return f; // f must be an array[]
});
export default () => {
const randomUser = data[Math.floor(Math.random() * data.length)];
console.log(`${randomUser.accountreference}`);
const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer blahblahPk9tjDdQ`,
},
};
const data1 = "{ \"amount\": 20000, \"external_reference\": \"mba-bcbdac6-024-m-2155667\", \"transaction_attributes\": { \"channel\": \"POP\"}}";
// execute
let res = http.post(url,data1,params);
check(res, {
"Create user response status code is 201": (r) => r.status == 201,
}
);
// Short break between iterations
sleep(1);
};
results
为了在 javascript 中使用模板字符串,您需要使用 ` 引号,而不是 ' 引号。
// Write this:
const url = `https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit`;
// Not this:
const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';
我正在尝试学习数据参数化。 我有一个包含 2 个条目的 json 文件:
[
{
"accountreference": "4157804914681"
},
{
"accountreference": "4157804925075"
}
]
我设法将它们打印到控制台中。但是,我无法进入 http.post.
这有什么问题吗: const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';
有更好的传递方式吗?
import http from "k6/http";
import { check, sleep } from "k6";
import { SharedArray } from 'k6/data';
// Test setup
export let options = {
stages: [
{ duration: '3s', target: 1 },
{ duration: '3s', target: 0 }, // scale down. Recovery stage.
],
thresholds: {
// 90% of requests must finish within 400ms, 95% within 800, and 99.9% within 2s.
http_req_duration: ['p(90) < 400', 'p(95) < 800', 'p(99.9) < 2000'],
},
};
const data = new SharedArray('accountRef', function () {
// here you can open files, and then do additional processing or generate the array with data dynamically
const f = JSON.parse(open('./accountRef.json'));
return f; // f must be an array[]
});
export default () => {
const randomUser = data[Math.floor(Math.random() * data.length)];
console.log(`${randomUser.accountreference}`);
const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';
const params = {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer blahblahPk9tjDdQ`,
},
};
const data1 = "{ \"amount\": 20000, \"external_reference\": \"mba-bcbdac6-024-m-2155667\", \"transaction_attributes\": { \"channel\": \"POP\"}}";
// execute
let res = http.post(url,data1,params);
check(res, {
"Create user response status code is 201": (r) => r.status == 201,
}
);
// Short break between iterations
sleep(1);
};
results
为了在 javascript 中使用模板字符串,您需要使用 ` 引号,而不是 ' 引号。
// Write this:
const url = `https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit`;
// Not this:
const url = 'https://api.example-test.com/user-api/accounts/${randomUser.accountreference}/benefit';