Add header to every request in Postman in pre-request script

前端 未结 7 2082
春和景丽
春和景丽 2021-02-05 07:00

I want to automatically add a header to every request in my whole collection using this pre-request script:

pm.request.headers.add({
    \'key\': \"myvar\",
             


        
7条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-05 07:44

    In test section of login, use this script to remember token in Environment

    var jsonData = JSON.parse(responseBody);
    
    tests["Body contains result"] = responseBody.has("result");
    
    var result = jsonData.result
    
    tests["result contains user"] = result.user !== null
    var user = result.user
    tests["result contains token"] = result.token !== null
    var token = result.token
    var accessToken = token.accessToken
    var refreshToken = token.refreshToken
    
    postman.setEnvironmentVariable("accessToken", accessToken);
    postman.setEnvironmentVariable("refreshToken", refreshToken);
    

    in every call wihich requires token, use token like this in header section

    Authorization = Bearer {{accessToken}}
    

提交回复
热议问题