The header configured on pre request script is not added

I am trying to use a pre-request to create an object then I will store an Id from that response and I will use in my main request but I am getting a status code 401 related to authentication but I added that header in the pre-request. could someone please help me?

Here is my script :

var options = {
  'method': 'POST',
  'url': 'https://www.pivotaltracker.com/services/v5/projects',
  'headers': {
    'X-TrackerToken': '12345647897',
    'Accept': ['application/json', 'application/json']
  },
  body: JSON.stringify({"name":"Asia"})
};
pm.sendRequest(options, function (error, response) { 
    const data = response.json();
    pm.variables.set("project_id", data.id);
    console.log('project id: '+ data.id);
}); 

attached image:

console log:

POST https://www.pivotaltracker.com/services/v5/projects401820 ms

POST /services/v5/projects HTTP/1.1

Content-Type: text/plain

User-Agent: PostmanRuntime/7.24.1

Accept: */*

Postman-Token: ee62474c-ff9c-4de8-95f9-20de17983a3d

Host: www.pivotaltracker.com

Accept-Encoding: gzip, deflate, br

Connection: keep-alive

Content-Length: 15

{"name":"Asia"}

HTTP/1.1 401 Unauthorized

Content-Type: application/json; charset=utf-8

Transfer-Encoding: chunked

Status: 401 Unauthorized

Cache-Control: no-cache

X-Request-Id: ac3fc051-c4f3-4fd6-9bc8-2ee99966f16b

X-Runtime: 0.008757

X-Frame-Options: SAMEORIGIN

X-Content-Type-Options: nosniff

X-Content-Type-Options: nosniff

Date: Wed, 13 May 2020 03:57:08 GMT

X-Powered-By: Phusion Passenger

Server: nginx + Phusion Passenger

Access-Control-Allow-Origin: *

Access-Control-Allow-Credentials: false

Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

Access-Control-Allow-Headers: X-TrackerToken,DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,X-Tracker-Warn-Unless-Project-Version-Is

X-Tracker-Client-Pinger-Interval: 20

Strict-Transport-Security: max-age=31536000; includeSubDomains; preload

X-XSS-Protection: 1; mode=block

Via: 1.1 google

Alt-Svc: clear

{"code":"unauthenticated","kind":"error","error":"Needs authentication credentials.","possible_fix":"Try basic auth, or include header X-TrackerToken.","general_problem":"User authentication required but not provided."}

Show pretty log

project id: undefined
1 Like

Hey @yuri.vayne,

Welcome to the community!! :rocket:

The key for adding Request Headers is header and not headers:

'header': {
	'X-TrackerToken': '12345647897',
	'Accept': 'application/json'
}

Could you give that a go and so if that helps out? :grin:

Hi @dannydainton, thanks ,it works the header was added in the request, but I am getting another problem related to body in the same request, could you please help me with this, not sure if that is the correct way to set the body :
body: JSON.stringify({“name”:“Kaylee”})

Is this the same if you add the 'Content-Type': 'application/json' header?

Looks like the docs say that it’s needed:

https://www.pivotaltracker.com/help/api/rest/v5#Projects

You are right, thanks @danny-dainton ,now its working well.

1 Like

Wow! “Header” vs “Headers” ??? I sure hope there is a good reason for 2 different keys because I spent hours debugging this one.
Thanks!!!

1 Like

agreed, the autocomplete it say ‘Headers’ only