Postman中运行的集合中的多个API响应自动保存为XML文件

qfe3c7zg  于 2023-10-18  发布在  Postman
关注(0)|答案(1)|浏览(187)

我是新的 Postman ,我试图保存多个响应的一个API调用,我正在运行对不同的值,我想知道是否有一种方法来自动保存每个响应本地作为一个XML文件,而不是我必须手动保存每个响应。
现在我只是去每一个响应,手动复制到一个文本编辑器,并保存它,但这是恼人的,当你有数百个响应。

wz8daaqr

wz8daaqr1#

可以使用Newman JavaScript库。
使用纽曼作为一个库运行 Postman 收集node+纽曼运行事件的响应保存到文件。

演示步骤

通过SOAP API使用simple calculator进行演示。我将调用四个操作(+,/,*,-)
SOAP URL

http://www.dneonline.com/calculator.asmx

在header中,Content-Type应该是text/xml; charset=utf-8(而不是application/xml)

在POST的主体中,要求添加

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Body>
        <Add xmlns="http://tempuri.org/">
            <intA>14</intA>
            <intB>7</intB>
        </Add>
    </soap:Body>
</soap:Envelope>

第一步

使用REST API调用制作Postman集合

步骤二

导出集合集合文件名

simple-calculator.postman_collection.json

这是全套

{
    "info": {
        "_postman_id": "2700a5f9-223a-4641-9d97-0db0c88465fd",
        "name": "simple-calculator",
        "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
        "_exporter_id": "1826150"
    },
    "item": [
        {
            "name": "addition",
            "protocolProfileBehavior": {
                "disabledSystemHeaders": {
                    "content-type": true
                }
            },
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "text/xml; charset=utf-8",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n    <soap:Body>\r\n        <Add xmlns=\"http://tempuri.org/\">\r\n            <intA>14</intA>\r\n            <intB>7</intB>\r\n        </Add>\r\n    </soap:Body>\r\n</soap:Envelope>",
                    "options": {
                        "raw": {
                            "language": "xml"
                        }
                    }
                },
                "url": {
                    "raw": "http://www.dneonline.com/calculator.asmx",
                    "protocol": "http",
                    "host": [
                        "www",
                        "dneonline",
                        "com"
                    ],
                    "path": [
                        "calculator.asmx"
                    ]
                }
            },
            "response": []
        },
        {
            "name": "division",
            "protocolProfileBehavior": {
                "disabledSystemHeaders": {
                    "content-type": true
                }
            },
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "text/xml; charset=utf-8",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n    <soap:Body>\r\n        <Divide xmlns=\"http://tempuri.org/\">\r\n            <intA>14</intA>\r\n            <intB>7</intB>\r\n        </Divide>\r\n    </soap:Body>\r\n</soap:Envelope>",
                    "options": {
                        "raw": {
                            "language": "xml"
                        }
                    }
                },
                "url": {
                    "raw": "http://www.dneonline.com/calculator.asmx",
                    "protocol": "http",
                    "host": [
                        "www",
                        "dneonline",
                        "com"
                    ],
                    "path": [
                        "calculator.asmx"
                    ]
                }
            },
            "response": []
        },
        {
            "name": "multiplication",
            "protocolProfileBehavior": {
                "disabledSystemHeaders": {
                    "content-type": true
                }
            },
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "text/xml; charset=utf-8",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n    <soap:Body>\r\n        <Multiply xmlns=\"http://tempuri.org/\">\r\n            <intA>14</intA>\r\n            <intB>7</intB>\r\n        </Multiply>\r\n    </soap:Body>\r\n</soap:Envelope>",
                    "options": {
                        "raw": {
                            "language": "xml"
                        }
                    }
                },
                "url": {
                    "raw": "http://www.dneonline.com/calculator.asmx",
                    "protocol": "http",
                    "host": [
                        "www",
                        "dneonline",
                        "com"
                    ],
                    "path": [
                        "calculator.asmx"
                    ]
                }
            },
            "response": []
        },
        {
            "name": "subraction",
            "protocolProfileBehavior": {
                "disabledSystemHeaders": {
                    "content-type": true
                }
            },
            "request": {
                "method": "POST",
                "header": [
                    {
                        "key": "Content-Type",
                        "value": "text/xml; charset=utf-8",
                        "type": "text"
                    }
                ],
                "body": {
                    "mode": "raw",
                    "raw": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\r\n    <soap:Body>\r\n        <Subtract xmlns=\"http://tempuri.org/\">\r\n            <intA>14</intA>\r\n            <intB>7</intB>\r\n        </Subtract>\r\n    </soap:Body>\r\n</soap:Envelope>",
                    "options": {
                        "raw": {
                            "language": "xml"
                        }
                    }
                },
                "url": {
                    "raw": "http://www.dneonline.com/calculator.asmx",
                    "protocol": "http",
                    "host": [
                        "www",
                        "dneonline",
                        "com"
                    ],
                    "path": [
                        "calculator.asmx"
                    ]
                }
            },
            "response": []
        }
    ]
}

步骤三

创建写文件(.js)
完整的演示代码
保存resultSaveFile.js

const newman = require('newman'); // require newman in your project
const fs = require('fs');

// call newman.run to pass `options` object and wait for callback
newman.run({
    collection: require('./simple-calculator.postman_collection.json'),
    reporters: 'cli'
}).on('request', (error, data) => {
    if (error) {
        console.log(error);
    }
    const fileName = `${data.item.name}.XML`;
    fs.writeFile(fileName, data.response.stream.toString(), function (error) {
        if (error) {
            console.error(error);
        }
    })
})

步骤四

安装依赖项

npm install newman fs

步骤五

node resultSaveFile.js
结果

将保存四个文件(addition.XML、division.XML、multiplication.XML和subtraction.XML)
addition.XML

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <soap:Body>
        <AddResponse xmlns="http://tempuri.org/">
            <AddResult>21</AddResult>
        </AddResponse>
    </soap:Body>
</soap:Envelope>

相关问题