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

Postman API测试:变量已生成但无法正常传递问题求助

Troubleshooting Your Postman Get Request Issue

Hey there! Let's break down why your Get request is returning all data instead of the specific resource you're targeting. Here are the most likely issues and how to fix them:

1. You're passing the ID in the wrong place (request headers instead of URL)

Most standard REST APIs expect resource IDs to be passed in the URL (either as a path parameter or query parameter), not in request headers. For example:

  • If your API uses path parameters (the most common approach for single resource requests), your Get URL should look like this:
    https://your-api-url.com/todos/{{todo2}}
    
  • If it uses query parameters, the URL would be:
    https://your-api-url.com/todos?id={{todo2}}
    

Putting {{todo2}} in request headers won't work unless your API explicitly requires that (which is rare for basic Get requests). This is probably why the API is ignoring the header and returning all data by default.

2. The environment variable wasn't saved correctly

Even though your test code looks valid, let's confirm the variable was actually stored properly:

  • Run your Post request, then check the Test Results tab below the response to ensure the "Status code is 201" test passed. If it failed, the variable won't be set.
  • Open the environment variables panel (click the eye icon in Postman's top right corner) and verify that todo2 has a value matching the ID from your Post response.

You can also enhance your test code to add a safety check for the id property, which will catch cases where the response doesn't include it:

pm.test("Status code is 201", function () {
    pm.response.to.have.status(201);
    var jsonData = pm.response.json();
    // Make sure the response contains an id field before saving it
    pm.expect(jsonData).to.have.property('id');
    pm.environment.set('todo2', jsonData.id);
});

3. You're using the wrong environment or variable name

Double-check these details:

  • Did you select the correct environment from the top right dropdown in Postman? If you saved todo2 to Environment A but have Environment B selected for your Get request, the variable will be empty.
  • Did you type the variable name correctly? Ensure {{todo2}} matches exactly what you used in pm.environment.set('todo2', ...) (capitalization and spelling matter!).

4. The API doesn't support passing the ID via headers (if you intended to)

If you're certain the API expects the ID in headers (e.g., a custom header like X-Todo-ID), confirm:

  • The header key matches exactly what the API documentation specifies.
  • The header value is set to {{todo2}} correctly (no typos, and the variable is populated with a valid ID).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 07:42:41