Postman预定脚本以读取从CSV文件的变量并附加到JSON主体
我有一个带有模板的JSON主体,可以传递到Postman/Newman中的API端点,如下所示:
{
"total": [
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
},
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
}
}
双括号中包含的所有内容都是一个需要从外部CSV文件读取的变量,该变量看起来像:
p,yyyymmdd,yyyy,mmdd
1,19991231,1999,1231
2,20001230,2000,1230
- 请注意,CSV不一定是CSV 2个条目很长 - 我理想地喜欢它可以接管最后一个添加的动态数量的条目。
- 理想情况下,我会摆脱CSV文件中的最后两列(
yyyy
和mmdd
),并让代码只需根据需要进行切片即可。
我什至如何开始编码预先要求的脚本?
即使无法从外部CSV读取,甚至不可能在预要求脚本中编码来执行此操作,JavaScript或Python中的快速单线可以很快就可以为我提供所需的输出将其放在请求主体中?
我什至尝试使用Postman Runner进行“假”跑步,以便我可以快速提取不同的JSON身体,但是即使这样,它也没有干净的方法来进行导出...
I have a JSON body with template to pass to an API endpoint in Postman/Newman as follows:
{
"total": [
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
},
{
"var1a": "ABCDE",
"var1b": {
"var2a": "http://url/site/{{yyyy}}/{{mmdd}}/{{yyyymmdd}}.zip",
"var2b": {
"var3a": "{{yyyymmdd}}",
"var3b": "GKG"
},
"var2c": "ABCDE"
"var2d": "{{p}}"
}
}
}
Everything enclosed in double braces is a variable that needs to read from an external CSV file, which looks like:
p,yyyymmdd,yyyy,mmdd
1,19991231,1999,1231
2,20001230,2000,1230
- Note that the CSV isn't necessarily 2 entries long - I'd ideally like it to take in a dynamic number of entries that append after the last.
- Ideally, I'd get rid of the last two columns in the CSV file (
yyyy
andmmdd
) and have the code just do a slice as needed.
How do I even begin coding the pre-request script?
Even if reading from an external CSV is not possible, or even coding within the pre-request script to do this, what is a quick one-liner in Javascript or Python that can quickly give me the output I need so that I can just manually place it in the request body?
I even tried using the Postman runner to do a "fake" run so that I can quickly extract the different JSON bodies, but even with this, there is no clean way that it does the export...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从CSV读取数据并不是解决问题的好方法,因为每个记录都将用于一个请求。您将整个记录都带入一个请求中。
我认为以前脚本编写代码将起作用。
在身体选项卡中:
在pre-rquest选项卡中:
Reading data from CSV is not the good way to solve problem because each record is will be used to one request. You want the whole record into ONE request.
I think write code in pre-request script will work.
In body tab:
In pre-rquest tab: