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

Postman:保存请求头复用及提取响应ID用于编辑请求的方法

Hey there! Let's break down your Postman questions with practical, easy-to-follow steps:

1. 如何在Postman中保存请求头并在后续请求中使用?

You’ve got two reliable ways to reuse request headers—pick the one that fits your workflow:

方法一:用环境/全局变量单独保存请求头

  • Open your target request and switch to the Headers tab
  • Find the header you want to reuse (like Authorization or Content-Type), right-click it, and select Set as variable
  • In the pop-up window, choose to save it as an environment variable (great for project-specific or environment-specific headers) or global variable (works across all your requests). Give it a clear name (e.g., projectAuthHeader) and confirm
  • In any subsequent request’s Headers tab, just type {{yourVariableName}} to pull in the saved header value

方法二:Set preset headers for a request collection

If a group of requests all need the same headers, using a collection preset is way more efficient:

  • Select your request collection, click the ... menu on the right, and choose Edit
  • Switch to the Headers tab in the collection editor, then add the header key-value pairs you want to reuse
  • All requests under this collection will automatically inherit these headers. If a specific request needs a custom override, just edit its own Headers tab to overwrite the preset

2. 提取响应中的ID用于后续编辑请求

This is super straightforward with Postman’s Tests script feature. Here’s how to do it:

  1. Open your user creation request and switch to the Tests tab
  2. Paste this script (since your response is JSON-formatted, we can parse it directly):
// Parse the response body into a JSON object
const userData = pm.response.json();
// Extract the `id` field and save it as an environment variable (use pm.globals.set for global variables)
pm.environment.set("targetUserId", userData.id);
  1. Send the user creation request. Once it succeeds, the targetUserId variable will be automatically set
  2. Open your edit-user request, then use {{targetUserId}} wherever you need the ID:
    • For example, if your edit API URL is /api/users/{{targetUserId}}, the variable will auto-replace with the extracted ID
    • If your request body needs the ID, write something like {"id": "{{targetUserId}}", "name": "Updated Tom"}

Pro tip: Environment variables are better than global variables here—they avoid conflicts between different test scenarios!


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:18:17