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

如何用循环生成的变量在Postman中批量发起删除ID请求?

Solution for Batch ID Deletion in Postman

Got it, let's fix this batch deletion issue. Since your server only allows deleting one ID per request, we can use Postman's pre-request and test scripts to loop through your target IDs and send individual requests for each one. Here's how to set it up:

Step 1: Configure the Pre-request Script

First, we'll define our list of IDs to delete and initialize a counter to track progress. Paste this code into the Pre-request Script tab of your request:

// Store the array of IDs we need to delete in a global variable
pm.globals.set("deleteIds", JSON.stringify([1, 2]));
// Start with the first ID by setting an initial index of 0
pm.globals.set("currentIdIndex", 0);

Step 2: Add the Test Script to Loop Through Requests

Next, we'll use the Test tab to send a deletion request for each ID, update our counter, and repeat until all IDs are processed. Add this code:

// Retrieve our stored ID list and current index from global variables
const deleteIds = JSON.parse(pm.globals.get("deleteIds"));
let currentIndex = parseInt(pm.globals.get("currentIdIndex"));

// Check if there are remaining IDs to process
if (currentIndex < deleteIds.length) {
    const targetId = deleteIds[currentIndex];
    // Build the full URL for the deletion request
    const requestUrl = `https://ids.com/${targetId}`;

    // Send the deletion request (adjust method/headers to match your server's requirements)
    pm.sendRequest({
        url: requestUrl,
        method: 'DELETE', // Switch to POST/PUT if your server uses a different deletion method
        header: {
            'Content-Type': 'application/json' // Add any required auth or custom headers here
        }
    }, function (err, res) {
        // Log results for debugging purposes
        console.log(`Processed ID ${targetId}: ${err ? 'Failed' : `Success (Status ${res.code})`}`);
    });

    // Update the index to point to the next ID
    currentIndex++;
    pm.globals.set("currentIdIndex", currentIndex);

    // Trigger the current request to run again for the next ID
    pm.execution.setNextRequest(pm.info.requestName);
} else {
    // Clean up global variables once all deletions are done
    pm.globals.unset("deleteIds");
    pm.globals.unset("currentIdIndex");
    console.log("All batch deletion requests completed successfully!");
}

How This Works

  • The pre-request script sets up our target ID list and starting point, so the test script knows exactly what to process.
  • Every time the request runs, the test script checks if there are IDs left to delete. If yes, it sends a dedicated request for that specific ID.
  • We increment our index and tell Postman to re-run the same request, repeating until every ID in our list is handled.
  • Once all deletions are done, we clean up the global variables to avoid leftover data cluttering your environment.

Quick Notes

  • Adjust the method in pm.sendRequest to match what your server expects (e.g., POST if deletion uses that verb instead of DELETE).
  • Add any required authentication headers (like API tokens) to the header object if your server requires them.
  • Modify the deleteIds array in the pre-request script to include any IDs you need to batch delete in the future.

内容的提问来源于stack exchange,提问作者Alex Tumanov

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 07:48:44