在Linux下,Postman本身不支持直接自定義HTTP頭部
你可以使用curl
命令行工具來發送帶有自定義頭部的HTTP請求。例如,以下命令將發送一個GET請求到指定的URL,并添加一個名為X-Custom-Header
的自定義頭部:
curl -H "X-Custom-Header: your_custom_value" https://your_api_endpoint
你還可以編寫一個簡單的腳本來發送帶有自定義頭部的HTTP請求。以下是一個使用Python編寫的示例:
import requests
url = 'https://your_api_endpoint'
headers = {
'X-Custom-Header': 'your_custom_value'
}
response = requests.get(url, headers=headers)
print(response.text)
雖然Postman本身不支持直接在請求中添加自定義頭部,但你可以使用預請求腳本來設置環境變量。這是一個示例:
const customHeader = 'your_custom_value';
pm.environment.set('X-Custom-Header', customHeader);
your_custom_value
替換為你的自定義頭部值。const response = pm.sendRequest({
method: 'GET',
url: pm.environment.get('X-Custom-Header') + 'your_api_endpoint'
});
your_api_endpoint
替換為你的API端點。這樣,你就可以在Postman的請求中使用自定義頭部了。請注意,這種方法實際上是在請求中使用了環境變量,而不是直接在請求中添加自定義頭部。