简体   繁体   中英

Adding multiple request headers in test script (Postman)

I am running a for loop with multiple requests in order to extract information from our API. The only problem is, I get console errors saying token and client headers are required to view response. I can only add one header in postman though in the test script. Is there something I'm not seeing?

for (k = 0; k < id.length; k++) {
    const emailRequest = {
        url: "" + id[k] + "/products",
        method: "GET",
        header: [{
            'key': "X-Auth-Token",
            "value": "",
        }
        ],  
        body:{
                mode: 'application/json',
                raw: JSON.stringify({
                    client_id: '',
                })
            }
    };
}

The header is not an Array. It is passed as an object with one name-value pair per header. The header should be given as below

 header: {
        'X-AUTH-TOKEN': 'mytoken',
        'Content-Type': 'application/json'
    }

Why don't you use multiple headers and variable to achieve this - then you can manipulate variables with scripts as you like.

Image

Useful Links: https://learning.getpostman.com/docs/postman/environments_and_globals/variables/

I have something which can help you: my requirements:

   key          value
"X-Auth-Token"  "38432904832904"
"Content-Type"  "application/json"

Solution: add this script in Pre-request script

pm.request.headers.add({
    key:"X-Auth-Token",
    value:"38432904832904"
});
pm.request.headers.add({
    key:"Content-Type",
    value
}) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM