You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Postman Runner或Newman批量迭代上传图片/文件

Absolutely, you can batch upload files/images in Postman Runner or Newman—here's how to make it work, even though Runner doesn’t natively support file variables in form-data. The solution relies on using text-based iteration data (CSV/JSON) for file paths and dynamic scripts to update your form-data request on each run.

1. Batch Upload in Postman Runner

Follow these steps to leverage Runner with file uploads:

Step 1: Prepare Iteration Data

Create a CSV or JSON file that lists the absolute paths (or relative paths to your Postman working directory) of the files you want to upload. For example, a CSV named upload-data.csv:

filePath
C:/Documents/images/photo1.jpg
C:/Documents/images/photo2.png
C:/Documents/docs/report.pdf

Step 2: Set Up Your Postman Request

  • Create a POST request with form-data body type.
  • Add a field (e.g., named image or file) and select any temporary local file (this will be replaced by the script later).

Step 3: Add a Pre-request Script

Go to the Pre-request Script tab and add this code to dynamically swap the file path from your iteration data:

// Fetch the file path from the current iteration row
const targetFilePath = pm.iterationData.get("filePath");
// Get the form-data body of your request
const formData = pm.request.body.formdata;
// Locate the file field you want to update (match the name you used in form-data)
const fileField = formData.get("image");

// Update the field to use the current iteration's file path
fileField.src = targetFilePath;

Step 4: Run the Collection with Runner

  • Open the Collection Runner, select your collection, and load your upload-data.csv as the iteration data.
  • Start the run—each iteration will automatically pick the next file from your data and upload it.
2. Batch Upload Using Newman

Newman supports the same pre-request script approach as Runner, plus offers more flexible script-based control. Here are two methods:

Method 1: Use Iteration Data + Pre-request Script

This is identical to the Runner workflow, but executed via the command line:

newman run your-collection.json -d upload-data.csv

Note: Ensure file paths in your CSV are absolute or relative to the directory where you run this command.

Method 2: Custom Node.js Script for Full Control

If you need more flexibility (like conditional uploads or dynamic file selection), write a Node.js script that calls Newman programmatically:

const newman = require('newman');
const path = require('path');

// Define your list of files to upload (adjust field names and paths as needed)
const uploadQueue = [
  { fieldName: 'image', filePath: path.resolve(__dirname, 'images/photo1.jpg') },
  { fieldName: 'image', filePath: path.resolve(__dirname, 'images/photo2.png') },
  { fieldName: 'file', filePath: path.resolve(__dirname, 'docs/report.pdf') }
];

// Loop through each file and run the request
uploadQueue.forEach((item, index) => {
  newman.run({
    collection: require('./your-collection.json'), // Path to your exported collection
    environment: require('./your-environment.json'), // Optional: your env file
    preRequestScript: function () {
      // Update the form-data field with the current file
      const formData = pm.request.body.formdata;
      const targetField = formData.get(item.fieldName);
      targetField.src = item.filePath;
    },
    reporters: 'cli' // Show output in command line
  }, (err) => {
    if (err) throw err;
    console.log(`Iteration ${index + 1}: Uploaded ${item.filePath} successfully!`);
  });
});

Run the script with:

node newman-upload-script.js
3. Critical Tips to Avoid Issues
  • File Paths: Always use absolute paths when possible—relative paths depend on the working directory of Postman/Newman, which can cause errors if not consistent.
  • Field Names: Double-check that the field name in your script (formData.get("image")) matches exactly what you’ve set in your form-data request.
  • Timeout Settings: For large files, increase the request timeout in Postman (Settings > General > Request Timeout) or add --timeout to your Newman command (e.g., newman run ... --timeout 60000 for 60 seconds).

内容的提问来源于stack exchange,提问作者chandan prasad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 16:27:44