0

当我尝试使用附有文档的 POST 方法时,我遇到了 Postman/Node.js 的问题。我想在请求之前更改文件的名称,因为无法上传两个同名的文件。我希望能够设置它,以便我的大学可以随时运行一个集合。我从http://blog.getpostman.com/2017/09/21/run-collections-with-file-uploads-using-newman/为 Postman 做了一个解决方法,用于文件上传自动化。

我的问题是:

如何获取文件,将其名称更改为随机名称,例如 test{{$timestamp}}.docx 并再次指向该 .json 文件中的新名称以便上传?

这是我的 .json 文件

{
        "name": "Save case's documents CID CDN",
        "event": [
            {
                "listen": "test",
                "script": {
                    "id": "04bf1f30-c2bc-4250-97c9-9efb7afa25e3",
                    "type": "text/javascript",
                    "exec": [
                        "// Check the status of a request",
                        "pm.test(\"Addition of a document successful!\", function () {",
                        "    pm.response.to.have.status(200);",
                        "});",
                        "// Check the response time ",
                        "pm.test(\"Response time is acceptable\", function () {",
                        "    pm.expect(pm.response.responseTime).to.be.below(2000);",
                        "});",
                        "// Set documentName variable",
                        "var jsonData = JSON.parse(responseBody);",
                        "postman.setEnvironmentVariable(\"documentName\", jsonData.content.documentName);"
                    ]
                }
            },
            {
                "listen": "prerequest",
                "script": {
                    "id": "09a922cd-6c2a-47cb-b597-d5dcab0d45bd",
                    "type": "text/javascript",
                    "exec": [
                        "",
                        ""
                    ]
                }
            }
        ],
        "request": {
            "auth": {
                "type": "bearer",
                "bearer": [
                    {
                        "key": "token",
                        "value": "",
                        "type": "string"
                    }
                ]
            },
            "method": "POST",
            "header": [
                {
                    "key": "Authorization",
                    "value": "{{token}}"
                },
                {
                    "key": "Content-Type",
                    "value": "application/x-www-form-urlencoded"
                }
            ],
            "body": {
                "mode": "formdata",
                "formdata": [
                    {
                        "key": "document",
                        "value": 
                         "C:\\Users\\User123456\\Desktop\\test.docx",
                        "description": "",
                        "type": "file",
                        "src": "C:\\Users\\User123456\\Desktop\\test.docx"
                    },
                    {
                        "key": "documentCategory",
                        "value": "Other",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "refCreatedBy",
                        "value": "{{bu_id}}",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "createdAt",
                        "value": "{{date}}",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "accuracy.status",
                        "value": "string",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "accuracy.lastModifiedDate",
                        "value": "{{$timestamp}}",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "accuracy.lastModifiedBy",
                        "value": "{{bu_id}}",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "quality.status",
                        "value": "sting",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "quality.lastModifiedDate",
                        "value": "{{$timestamp}}",
                        "description": "",
                        "type": "text"
                    },
                    {
                        "key": "quality.lastModifiedBy",
                        "value": "{{$timestamp}}",
                        "description": "",
                        "type": "text"
                    }
                ]
            },
            "url": {
                "raw": "{{site}}/cases/{{case_id}}/documents/{{documentName}}",
                "host": [
                    "{{site}}"
                ],
                "path": [
                    "cases",
                    "{{case_id}}",
                    "documents",
                    "{{documentName}}"
                ]
            },
            "description": ""
        },
        "response": []
    }
]

}

4

0 回答 0