POSTMAN 除了可以發送 request 到 server 之外,也有提供 pre-request script 的功能,讓我們可以撰寫 script 來進行一連串的測試。
舉個例子,我們有三支 API,邏輯為 名稱(Request body)【Response body】:
- 登入(帳號、密碼)【Token】
- 取得使用者的消費紀錄列表(Token)【Array<Receipt ID>】
- 取得單筆消費紀錄(Receipt ID)【Receipt】
在沒有撰寫 pre-request 之前若要取得 Receipt 的話,我們得要先呼叫 登入,接著手動複製 Token 到 取得使用者的消費紀錄列表 的 request body 裡頭,然後再複製⋯⋯(你們應該懂)
這邊就來教教如何先寫好 script,之後只要按最後你想得到的那支 API 就行了!
let account = 'account' let password = 'password' let index = 0 pm.sendRequest({ url: 'https://Archie.tw/login?account=' + account + '&password=' + password, method: 'get' }, function(err, response) { let token = response.json().token; pm.sendRequest({ url: 'https://Archie.tw/getReceiptList?token=' + token, method: 'get' }, function(err, response) { pm.environment.set("receiptIdentifier", response.receiptIdentifier); }); });
接著在 Params / Body 那邊就可以使用剛剛所定義的環境變數({{receiptIdentifier}})了!
然後請不要真的打範例的 url,是不會有東西回給你的(真的)。
I believe that is among the such a lot vital info for me.
And i’m glad reading your article. However should statement on some general issues, The site style is great, the articles is truly nice
: D. Just right activity, cheers
Good day! Do you use Twitter? I’d like to follow you if that would be okay.
I’m definitely enjoying your blog and look forward to new posts.
My twitter account is @ChangArchie👍