3

I'm using sendRequest function to send a 2nd request as part of the Test for my 1st request. But it seems that sendRequest does not use the Headers I specify in the function. Any ideas why and how to fix it?

Here is part of my Test, that sends 2nd request:

var runHost = pm.environment.get("MyHost");
var runToken = pm.environment.get("Token");
pm.sendRequest({
    url: runHost,
    method: 'PUT',
    headers: {
        "Authorization": "Auth "+runToken,
        "Accept": "application/json",
        "Content-Type": "application/json"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(jsonData)
    }
}, (err, res) => {
    console.log(res);
});

Here is what I see as an actual Request Headers sent (what I see in Console):

PUT https://some_url
Request Headers:
     Content-Type:"text/plain"
     User-Agent:"PostmanRuntime/7.15.2"
     Accept:"*/*"
     Cache-Control:"no-cache"
     Postman-Token:"5e3543c-1ww0-dfc4-bert-92ba9a455667"
     Host:"my_host"
     Accept-Encoding:"gzip, deflate"
     Content-Length:1876
     Connection:"keep-alive"

I expect Request Headers to have the following attributes and values:

Authorization:"Auth current_token_value"
Accept:"application/json"
Content-Type:"application/json"
...
KVN
  • 863
  • 1
  • 17
  • 35

1 Answers1

2

The key for the object that contains the Request Headers should be header, like in the example below:

pm.sendRequest({
    url: runHost,
    method: 'PUT',
    *header*: {
        "Authorization": "Auth "+runToken,
        "Accept": "application/json",
        "Content-Type": "application/json"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(jsonData)
    }
}, (err, res) => {
    console.log(res);
});
Danny Dainton
  • 23,069
  • 6
  • 67
  • 80
  • The same issue. Header is specified correctly. Postman overwrites Header completely (unexpected), while leaves the body untouched (expected). Reproducing on the public helloworld Postman sample didn't produce the results, as everything there works fine. Seems like there are some cases where it won't take into account the headers set within pm.sendRequest – Omar Khazamov Dec 29 '21 at 22:00
  • 1
    It's probably worth creating your own question that includes all the details for your context. – Danny Dainton Dec 30 '21 at 08:09