I am currently facing an issue while trying to make a GET API request using protractor. The challenge lies in using the bearer token generated from a previous POST response in the headers of the GET request. Although I have successfully executed the POST request, I am struggling to incorporate the generated token in the GET request headers. Below is the snippet of code that I have attempted. I would appreciate it if someone could guide me on the correct syntax for this approach.
Note: I have masked the URL and credentials for confidentiality reasons.
var Request = require("request");
describe('post user request', () => {
it('create user test', (done) => {
//1. create user (POST)
Request.post({
// method: 'POST',
"url": "http://example.com",
"body" : {
"username": "abc",
"password": "abc1",
}
}).then((res)=>{ console.log(JSON.stringify(res))
}).then((res) =>{
const token1 = res.token
//2. get user (GET)
Request.get({
// method: 'GET',
"url": "http://example.com/xyz",
"headers": {
"Authorization" : "Bearer " + token1
}
}).then((res)=>{
console.log(res)
done();
})
})
}) })
Error message :
F
post user request
× create user test
- Failed: Argument error, options.body.
Failures:
1) post user request create user test
Message:
Failed: Argument error, options.body.
Stack:
Error: Argument error, options.body.
at setContentLength (D:\Protractor\node_modules\request\request.js:437:28)
at Request.init (D:\Protractor\node_modules\request\request.js:442:5)
at new Request (D:\Protractor\node_modules\request\request.js:127:8)
at request (D:\Protractor\node_modules\request\index.js:53:10)
at Function.post (D:\Protractor\node_modules\request\index.js:61:12)
at UserContext.<anonymous> (D:\Protractor\Specs_Map\APIfile.spec.js:8:21)
at D:\Protractor\node_modules\jasminewd2\index.j