I just need to take the token returned in API to complete a URL. I need to take the response.body and save in a variable to use after.
I'm using the protractor to automation tests, and to open a URL I'm consuming an API that return a token, to be used as a parameter in a URL
describe('TEste API', function(){
var Request = require('request');
it("api Testing in protractor", async function (callback) {
let tokenReturn = "empty";
tokenReturn = Request.post({
"headers": { "content-type": "application/x-www-form-urlencoded" },
"url": "https://corpqa.sts.ford.com/adfs/oauth2/token",
"form": {
"grant_type":'client_credentials',
"client_id":'ad9cdf61-e863-4606-a90a-cf7b7141234',
"client_secret":'QmjeT5UZ0N1M0jOEcggrxgFzw-vrZY2UphAy21d5',
"resource":'urn:age:pcf:sss:dev',
}
}, (error, response, body) => {
if (error) {
return console.dir(error);
}
resp = JSON.parse(response.body);
console.log("inside: " + resp.access_token);
callback();
});
console.log("outside: www.example.com/?token=" + tokenReturn);
});
});//------------------------- end describe
In console show me.
outside:
www.example.com/?token=[object Object]
inside:
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IldzZks3Q2FtMDZKY3dkR1Z6a2NiYVUzd21wZyJ9.eyJhdWQiOiJ1cm46YWdlOnBjZjpzdGY6ZGV2IiwiaXNzIjoiaHR0cHM6Ly9jb3JwcWEuc3RzLmZvcmQuY29tL2FkZnMvc2VydmljZXMvdHJ1c3QiLCJpYXQiOjE1NzMyMzIzNjYsImV4cCI6MTU3MzIzNTk2NiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiYWQ5Y2RmNjEtZTg2My00NjA2LWE5MGEtY2Y3YjcxNDE4OTQ1IiwiYXV0aG1ldGhvZCI6Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9hdXRoZW50aWNhdGlvbm1ldGhvZC9wYXNzd29yZCIsImF1dGhfdGltZSI6IjIwMTktMTEtMDhUMTc6MDQ6MjYuMTU5WiIsInZlciI6IjEuMCJ9.kuVfmgvN7_t4h2LB5o6dzTV2hngdapMrWFRPANISg5ayUnqeBMKHI5PWvISddfZ2qjO7kSPXlYVffhjrBhqAxY75EAhLX8hAmHDm_2jl49prtnsnqV-l-zhFaqCyfhEcgtVCRE_GX6EON2pewsX09Vdbn_2uHvh5wcGdWCnontzkZdrf__X8-tuE5R7tHrtge0ZXMdx5bCF7INKzA1YolTwxOOiYNVvZFPDKLRwa4VUf_qTKN5BmLisRVN4gmnTzGTPXjXlHZApRwJAbXR4V7VhtVQ6VcjHuyYIpp_rK0K7kQjwu0FLpE1FHZTNRwvXNI1VqyhGaanx2bM_59NyDgg
I just want the outside result to be equal to the inside with token. I made some changes but the result change to UNDEFINED instead OBJECT.
Maybe use promises?
Example:
function requestToServer(){
return new Promise((resovle, reject) => {
Request.post({
"headers": { "content-type": "application/x-www-form-urlencoded" },
"url": "https://corpqa.sts.ford.com/adfs/oauth2/token",
"form": {
"grant_type":'client_credentials',
"client_id":'ad9cdf61-e863-4606-a90a-cf7b7141234',
"client_secret":'QmjeT5UZ0N1M0jOEcggrxgFzw-vrZY2UphAy21d5',
"resource":'urn:age:pcf:sss:dev',
}
}, (error, response, body) => {
if (error) {
return console.dir(error);
}
resp = JSON.parse(response.body);
console.log("inside: " + resp.access_token);
resovle(resp.access_token);
callback();
});
});
}
describe('TEste API', function(){
var Request = require('request');
it("api Testing in protractor", async function (callback) {
let tokenReturn = await requestToServer();
console.log("outside: www.example.com/?token=" + tokenReturn);
});
});//------------------------- end describe
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.