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

wvmv3b1j  于 2024-01-07  发布在  Postman
关注(0)|答案(1)|浏览(355)
  1. {
  2. "addStandard": [
  3. {
  4. "attachments": ["file1,file2,file3"],
  5. "organization": "3",
  6. "designationNubmer": "3",
  7. "recognition": "3",
  8. "title": "3",
  9. "generalUse": "3",
  10. // "applicant_info_id": 1,
  11. // "attachmentsId": 1
  12. },
  13. {
  14. "organization": "4",
  15. "designationNubmer": "4",
  16. "recognition": "4",
  17. "title": "4",
  18. "generalUse": "4",
  19. // "applicant_info_id": 1,
  20. //"attachmentsId": 2
  21. }
  22. ]
  23. }

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

mzsu5hc0

mzsu5hc01#

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


的数据
和这个指令。Json:

  1. {
  2. "addStandard": [
  3. {
  4. "attachments": [
  5. "file1.txt",
  6. "file2.txt",
  7. "file3.txt"
  8. ],
  9. "organization": "3",
  10. "designationNumber": "3",
  11. "recognition": "3",
  12. "title": "3",
  13. "generalUse": "3"
  14. },
  15. {
  16. "attachments": [
  17. "file4.png"
  18. ],
  19. "organization": "4",
  20. "designationNumber": "4",
  21. "recognition": "4",
  22. "title": "4",
  23. "generalUse": "4"
  24. }
  25. ]
  26. }

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

  1. import requests
  2. import json
  3. import mimetypes
  4. #file upload using https://pypi.org/project/requests/ based on json input
  5. #mimetype = 'text/plain'
  6. url = 'https://httpbin.org/post'
  7. # Load json with instructions
  8. instruction_file = open('payload/instructions.json')
  9. instruction_data = json.load(instruction_file)
  10. instruction_file.close()
  11. # Parse each payload
  12. for instruction in instruction_data['addStandard']:
  13. files = {}
  14. payload = instruction
  15. # Parse filelist
  16. for filename in instruction["attachments"]:
  17. #print(filename)
  18. filepath = 'payload/'+filename
  19. mimetype = mimetypes.guess_type(filepath)
  20. file_payload = open(filepath, 'rb')
  21. files[filename] = (filename, file_payload, mimetype, {'Expires': '0'})
  22. #file_payload.close() # this doesn't work! TODO: gather file handles and close them after sending
  23. r = requests.post(url, files=files, data=payload)
  24. print(r.text)
  25. #print(r.status_code)
  26. #TODO: close file handles here, before reading the next instruction set


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

展开查看全部

相关问题