နောက်ပြဿနာ တစ်ခုက JWT token တွေသုံးထားတဲ့အခါ login နဲ့ refresh token API တွေ execute တိုင်း bearer token တွေ manual လိုက်ထည့်ရတာပဲ။ အချိန်ကြာလာတော့ annoying ဖြစ်မိတယ်၊ အထူးသဖြင့် rush ဖြစ်နေတဲ့အချိန်မျိုးတွေ။

အများစုလုပ်ကြတာက bearer token ကို collection variable ထဲသွားထည့်တာဆိုတော့ server ပြောင်းစမ်းတဲ့အခါတွေမှာ override ဖြစ်ပြီး ခဏခဏ login ပြန်ဝင်ပြီး variable update လုပ်ရတာပဲ။

အဲဒိပြဿနာတွေ အကုန်လုံးကို postman script ရေးပြီး ရှင်းလို့ရတယ်။

Bearer token ကို Postman Script နဲ့ ဘယ်လို update လုပ်မလဲ

Step (1): Setup the token variable inside Authorization tab

Step (2): Create postman script inside Tests Tab

// prepare functions
function checkLoginOrRefresh (path) {
path = path.toLowerCase()
return path.includes('login') || path.includes('refresh')
}

// prepare variables
const paths = pm.request.url.path
const code = pm.response.code
const server = pm.environment.get('server')

// check api is login or refresh
let isLoginOrRefresh = (paths.filter(checkLoginOrRefresh)).length

if (code == 200 && isLoginOrRefresh) {
// get token return by backend
const token = pm.response.json().access_token

// set token by server and guard
const tokenName = server + 'Token'
pm.globals.set(tokenName, token)
}

ဘာလို့ Tests မှာရေးလဲဆိုတော့ API response ထဲက bearer token ကိုယူချင်လို့။ အောက်မှာ အသေးစိတ်ရှင်းပြပါမယ်။

// prepare functions
function checkLoginOrRefresh (path) {
path = path.toLowerCase()
return path.includes('login') || path.includes('refresh')
}

// prepare variables
const paths = pm.request.url.path
const code = pm.response.code
const server = pm.environment.get('server')

// check api is login or refresh
let isLoginOrRefresh = (paths.filter(checkLoginOrRefresh)).length

URL မှာ login or refresh ဆိုတဲ့ keyword ပါမှ token သိမ်းတဲ့ အလုပ်ကိုလုပ်ပါမယ်။ Project အလိုက် ကိုယ့်စိတ်ကြိုက်နာမည်ပြောင်းလို့ရပါတယ်။

if (code == 200 && isLoginOrRefresh) {
// get token return by backend
const token = pm.response.json().access_token

// set token by server and guard
const tokenName = server + 'Token'
pm.globals.set(tokenName, token)
}

API Success ဖြစ်ပြီး keyword လည်းပါခဲ့ရင် response data ထဲက bearer token ကို ဆွဲထုတ်။ ပြီးရင် environment မှာရွေးထားတဲ့အတိုင်း localToken, stagingToken or productionToken ဆိုပြီးထွက်မယ်။ အဲဒါကိုမှ global level မှာ variable ဆောက်လိုက်ပါတယ်။ အခုလိုခွဲသိမ်းတော့ server ပြောင်းတိုင်းမှာ token တွေ override မဖြစ်တော့ဘူး။

အမှန်ဆို collection level မှာ သိမ်းသင့်တာပါ၊ ဘာလို့မလုပ်လဲဆို collection မှာက manual create ပဲရတယ်၊ scriptကနေ dynamic create ပေးမလုပ်ဘူး။

Step (3): Modify existing postman script inside Pre-request Scripts Tab

if (pm.environment.has('server')) {
// prepare variables
const server = pm.environment.get('server')

// set backend url
const urlName = server + 'URL'
const baseURL = pm.variables.get(urlName)
pm.environment.set('baseURL', baseURL)

// get token by server (NEW!)
const tokenNameByServer = server + 'Token'
const token = pm.globals.get(tokenNameByServer)

// set auth token (NEW!)
const tokenNameForAuth = 'token'
pm.globals.set(tokenNameForAuth, token)
} else {
throw new Error("Please set the server variables by asking your backend developer!")
}

အပိုင်း ၂မှာ ရေးခဲ့တဲ့ Pre-request Scripts ကို မှတ်မိမယ် ထင်ပါတယ်။ API execute မလုပ်ခင် သက်ဆိုင်ရာ bearer token တွေ header ထဲထည့်ပေးဖို့ code နည်းနည်းထပ်ဖြည့်ရပါမယ်။

// get token by server (NEW!)
const tokenNameByServer = server + 'Token'
const token = pm.globals.get(tokenNameByServer)

// set auth token (NEW!)
const tokenNameForAuth = 'token'
pm.globals.set(tokenNameForAuth, token)

ဒါကတော့ အသစ်ဖြည့်လိုက်တဲ့ codes တွေပါ။

ရွေးထားတဲ့ Environment အရ သက်ဆိုင်ရာ token variable ကို global level က သွားဆွဲတယ်။ ပြီးတော့ global ထဲမှာပဲ token ဆိုတဲ့ နာမည်နဲ့ dynamic create လုပ်ပေးလိုက်တယ်။ ဆိုတော့ နောက်ဆုံး global ထဲ သွားကြည့်ရင် ဒီလိုမြင်ရမယ်။

Step (1) မှာ token variable ကို bearer token အနေနဲ့ သုံးထားခဲ့ပြီးဖြစ်လို့ manually update စရာမလိုပဲ အကုန်လုံးအချိတ်အဆက်မိနေမှာပါ။

Sign up to discover human stories that deepen your understanding of the world.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Myo Win Thein
Myo Win Thein

Written by Myo Win Thein

A developer sharing experiences with the world, one byte at a time.

No responses yet

Write a response