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

前端 未结 7 2083
春和景丽
春和景丽 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:43

    This copied from here, but it worked for me

    https://gist.github.com/madebysid/b57985b0649d3407a7aa9de1bd327990

    pm.sendRequest({
        url: "https://mydomain/ers/config/endpoint",
        method: 'GET',
        header: {
            'content-type': 'application/json',
            'accept': 'application/json',
            //'x-site-code': pm.environment.get("x-site-code")
            'X-CSRF-TOKEN': 'fetch'
        },
        body: {
            mode: 'raw'//,
            raw: JSON.stringify({ email: pm.environment.get("email"), password: pm.environment.get("password") })
        }
    }, function (err, res) {
    
        pm.environment.set("X-CSRF-TOKEN", "Bearer " + res.json().token);
    });
    

提交回复
热议问题