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
AuthorizationorContent-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:
- Open your user creation request and switch to the Tests tab
- 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);
- Send the user creation request. Once it succeeds, the
targetUserIdvariable will be automatically set - 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"}
- For example, if your edit API URL is
Pro tip: Environment variables are better than global variables here—they avoid conflicts between different test scenarios!
内容的提问来源于stack exchange,提问作者Tom Tom
相关产品推荐
相关产品推荐

