使用postman在表单数据中进行批量分发?

wvmv3b1j  于 12个月前  发布在  Postman
关注(0)|答案(1)|浏览(222)
{
        "addStandard": [
            {   
                "attachments": ["file1,file2,file3"],   
                "organization": "3", 
                "designationNubmer": "3", 
                "recognition": "3", 
                "title": "3", 
                "generalUse": "3",
                // "applicant_info_id": 1,
                // "attachmentsId": 1
            },
            {
                "organization": "4", 
                "designationNubmer": "4", 
                "recognition": "4", 
                "title": "4", 
                "generalUse": "4",
                // "applicant_info_id": 1,
                //"attachmentsId": 2
            }
        ]
    }

字符串
如何在form-data中批量添加上面的JSON对象?附件是文件对象的实际数组。
x1c 0d1x的数据
我不知道如何在表单数据中批量添加附件文件对象?我知道上面的截图是错误的,因为我也没有提到“addStandards”。

mzsu5hc0

mzsu5hc01#

你不会的
如果您想解析JSON,加载文件并发送它们,那么您所要求的就比Postman所能做的更多了。
您将不得不加载文件(我不相信 Postman 有能力,并附加他们。此外,您需要使用multipart/form-data(见How does HTTP file upload work?) Postman 不是正确的锤子为这个torx螺丝。
你需要使用真实的的脚本或编程语言来完成你想要的。
我做了一个简单的python脚本,它将解决您的问题:
给定此布局:


的数据
和这个指令。Json:

{
    "addStandard": [
        {
            "attachments": [
                "file1.txt",
                "file2.txt",
                "file3.txt"
            ],
            "organization": "3",
            "designationNumber": "3",
            "recognition": "3",
            "title": "3",
            "generalUse": "3"
        },
        {
            "attachments": [
                "file4.png"
            ],
            "organization": "4",
            "designationNumber": "4",
            "recognition": "4",
            "title": "4",
            "generalUse": "4"
        }
    ]
}

字符串
这个Python脚本将解析JSON,加载每个文件,检测mime类型,并将其上传。

import requests
import json
import mimetypes

#file upload using https://pypi.org/project/requests/ based on json input

#mimetype = 'text/plain'

url = 'https://httpbin.org/post'

# Load json with instructions
instruction_file = open('payload/instructions.json')
instruction_data = json.load(instruction_file)
instruction_file.close()

# Parse each payload
for instruction in instruction_data['addStandard']:
    files = {}
    payload = instruction
    # Parse  filelist
    for filename in instruction["attachments"]:
        #print(filename)
        filepath = 'payload/'+filename
        mimetype = mimetypes.guess_type(filepath)
        file_payload = open(filepath, 'rb')
        files[filename] =  (filename, file_payload, mimetype, {'Expires': '0'})
        #file_payload.close() # this doesn't work! TODO: gather file handles and close them after sending
    r = requests.post(url, files=files, data=payload)
    print(r.text)
    #print(r.status_code)
    #TODO: close file handles here, before reading the next instruction set


请注意,我认为有一个内存泄漏,因为我们调用open(),但从来没有关闭文件。我已经添加了评论,以这种效果这是没有问题的小列表和文件,但如果你上传的是mpg它将成为一个问题。您的问题没有说明任何文件扩展名,因此我假设较小的文件.

相关问题