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

Postman:修改变量值影响环境变量值,是Bug还是设计特性?

解决Postman中复用请求响应体并修改字段的方案

Hey there, let's break down how to handle this scenario properly—and figure out if you're actually hitting a bug or just a common workflow gotcha in Postman.

Your goal makes total sense: capture the full response from Request 1, then reuse that exact body as the base for subsequent PUT requests (only tweaking specific boolean fields each time). Here's how to implement this reliably:

1. Store the original response body in a dedicated variable

In Request 1's Tests tab, add this script to save the full JSON response to a collection variable (collection variables are ideal here since all your requests are likely in the same collection—avoids cluttering global variables):

// Request 1 Tests: Save full response to collection variable
const rawResponse = pm.response.json();
pm.collectionVariables.set("baseRequestBody", JSON.stringify(rawResponse));

We use JSON.stringify here because Postman stores variables as strings by default—this ensures we can easily parse it back into a manipulable object later.

2. Modify the base body for each PUT request

For every subsequent PUT request, add this script to the Pre-request Script tab. It will pull the original body, update your target boolean field, and set it as the request body:

// Pre-request Script for PUT requests
// Fetch the original base body and parse it back to an object
const baseBody = JSON.parse(pm.collectionVariables.get("baseRequestBody"));

// Modify your boolean field(s) here—example: toggle "isActive" to false
baseBody.isActive = false;

// Overwrite the request body with the modified object
pm.request.body.raw = JSON.stringify(baseBody, null, 2);

This way, every PUT request starts fresh with the original Request 1 response, not a modified version from a previous PUT.

3. Check if this is a bug or a workflow issue

If your current setup isn't working, it's probably not a Postman bug—here are the most common pitfalls to check:

  • Accidentally overwriting the base variable: If you have a script in your PUT request that saves the modified body back to baseRequestBody, you'll be using the updated version for future requests. Double-check your Tests tabs!
  • Invalid JSON parsing: If Request 1's response isn't valid JSON, pm.response.json() will throw an error. Check the Postman Console (View > Show Postman Console) for parsing errors.
  • Variable type mismatch: If you forgot to use JSON.stringify when saving, the variable will be a string instead of an object—you can't modify fields on a string!

If you still suspect a bug, try isolating the issue: create a new blank collection with just Request 1 and one PUT request, use the scripts above, and test again. If it works there, the problem is likely with other scripts or variables in your original collection.

内容的提问来源于stack exchange,提问作者Nico Verdonk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 06:37:10