0

I am experimenting on auto fetch access tokens in postman using a Pre-request Script. I have the following code to make a request to fetch and update access tokens.

var isExpired = pm.environment.get("token_expiration_time")
const body = {
    "refresh": pm.environment.get("refresh_token")
}
if (Date.now() > isExpired) {
    const postRequest = {
        url:  pm.variables.get("auth_url"), 
        method: 'POST',
        headers: {"Content-Type": "application/json"},
        body: body
    }
    pm.sendRequest(postRequest, (err, res) => {
        console.log(res.text())
        if (!res.status.code === 400) {
           pm.environment.set("access_token", res.access)
           pm.environment.set("token_created_time", Date.now())
           pm.environment.set(
                "token_expiration_time", 
                pm.environment.get("token_created_time") + parseInt(
                    pm.environment.get("access_token_expiration_delta")
                ) - 5000
            )
        }
        
    })
}

I keep getting the following error from the backend

{"refresh":["This field is required."]}

I'd appreciate you suggestions on possible fixes, thanks:)

1 Answers1

0

Solved this with the following

Header:

headers: {
   'Content-Type': 'multipart/form-data',
},

Body:

body: {
      mode: 'formdata',
      formdata: [{key: "refresh", value: pm.environment.get("refresh_token"), 
              disabled: false, description: {content:"", type:"text/plain"}}]
}