为后续调用设置标头变量 [英] Set headers variable for subsequent calls
问题描述
我有一个设置调用,返回的标头中的会话ID如下所示。
I have one setup call that returns session id in header as shown below.
curl -X GET --header 'Accept: application/json' 'https://<url>/initConn'
返回会话id作为响应标头的一部分,例如:
Returns a session id as part of response header as:
{ ...,
"sessionId": "111",
...
}
现在我必须在同一会话ID中使用所有后续调用为:
Now I have to use the same session id in all subsequent call as:
curl -X GET --header 'Accept: application/json' --header 'X-SESSION_ID: 111' 'https://<url>/someCommand'
使用邮递员,怎么可能:
Using postman, how could one:
- 从标题中提取会话ID
- 附加邮递员的后续呼叫
推荐答案
不知道如何使用 cURL $ c通过命令行进行操作$ c>,但是您可以使用
环境变量
设置从一个API请求获取标头,保存然后在另一个API调用中使用它。
Not sure how you would this through command line using cURL
, but you can use the Environment Variables
to set the get the header from one API request, save it and then use it in another API call.
假设我们有一个名为F1的API,它为我们提供了一个名为 uid
的参数,在我们的标头中将此 access_token
用作另一个API,称为F2
Suppose we have an API, called F1, which gives us a parameter called uid
in response, and we need to use this access_token
in our header for another API , called F2
- 转到在邮递员窗口的拐角处的
管理环境
。
- Go to the
Manage Environments
on the corner end of the Postman window.
- 点击
管理环境
,然后在在新窗口中,单击添加以添加新环境
- Click on
Manage Environment
and in the new window, click on Add to add a new environment
-
添加要添加的新环境变量名称。假设我希望将变量命名为
access_token
,因此我将向新添加的环境中添加一个新键,并将value字段保留为空。
Add the new environment variable name that you want to add. Suppose I want my variable to be called
access_token
, so I will add a new key, to the newly added environment and keep the value field empty.
现在点击 ADD
,然后转到邮递员窗口,输入API F1的API端点以及您需要随F1发送的所有标头值。确保从显示的环境选项中选择新创建的环境。
Now click on ADD
, and go to your Postman window, and enter the API endpoint for API F1, along with any header values that you need to send with the F1. Make sure you select the newly created environment from the environments options shown.
-
现在转到测试,它位于
标头的右侧
,然后在测试窗口中输入以下内容,如下所示
Now go to Tests, which is located to the right of the
headers
, and enter the following in the test window as shown below
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable( access_token,jsonData.uid);
现在点击 SEND
并等待F1的响应。
Now hit SEND
and wait for the response from F1.
- 打开另一个标签,输入F2的API端点,然后选择在上述步骤中刚刚创建的新环境。
现在,因为我们希望 uid
作为响应的一部分F2请求的标头中,我们需要将其添加到标头中。
Now, since we want the uid
from response of to be a part of the header of our request for F2, we need to add this in our header.
转到标头
标签,然后输入 uid
作为密钥,并 {{access_token}}
作为该密钥的值,如下所示。
Go to the header
tab, and enter the uid
as Key and {{access_token}}
as the value for this key, as show below.
现在,你们都准备提出F2的请求。这将从第一个API F1中提取 uid
,并将其作为API F2的标头传递
Now, you are all set to make the request for F2. This will extract uid
from the first API F1, and pass it as the header for API F2
这篇关于为后续调用设置标头变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!