将响应Request.post令牌保存在变量中

时间:2019-11-08 17:12:18

标签: javascript node.js post request protractor

我只需要获取API中返回的令牌即可完成URL。我需要将response.body并保存在变量中以供以后使用。

我正在使用量角器进行自动化测试,并打开一个URL,我正在使用一个返回令牌的API,该API将用作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

在控制台中向我展示。

外部:

www.example.com/?token=[object Object]

内部:

eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IldzZks3Q2FtMDZKY3dkR1Z6a2NiYVUzd21wZyJ9.eyJhdWQiOiJ1cm46YWdlOnBjZjpzdGY6ZGV2IiwiaXNzIjoiaHR0cHM6Ly9jb3JwcWEuc3RzLmZvcmQuY29tL2FkZnMvc2VydmljZXMvdHJ1c3QiLCJpYXQiOjE1NzMyMzIzNjYsImV4cCI6MTU3MzIzNTk2NiwiYXBwdHlwZSI6IkNvbmZpZGVudGlhbCIsImFwcGlkIjoiYWQ5Y2RmNjEtZTg2My00NjA2LWE5MGEtY2Y3YjcxNDE4OTQ1IiwiYXV0aG1ldGhvZCI6Imh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vd3MvMjAwOC8wNi9pZGVudGl0eS9hdXRoZW50aWNhdGlvbm1ldGhvZC9wYXNzd29yZCIsImF1dGhfdGltZSI6IjIwMTktMTEtMDhUMTc6MDQ6MjYuMTU5WiIsInZlciI6IjEuMCJ9.kuVfmgvN7_t4h2LB5o6dzTV2hngdapMrWFRPANISg5ayUnqeBMKHI5PWvISddfZ2qjO7kSPXlYVffhjrBhqAxY75EAhLX8hAmHDm_2jl49prtnsnqV-l-zhFaqCyfhEcgtVCRE_GX6EON2pewsX09Vdbn_2uHvh5wcGdWCnontzkZdrf__X8-tuE5R7tHrtge0ZXMdx5bCF7INKzA1YolTwxOOiYNVvZFPDKLRwa4VUf_qTKN5BmLisRVN4gmnTzGTPXjXlHZApRwJAbXR4V7VhtVQ6VcjHuyYIpp_rK0K7kQjwu0FLpE1FHZTNRwvXNI1VqyhGaanx2bM_59NyDgg

我只希望外部结果等于带有令牌的内部结果。 我进行了一些更改,但结果更改为UNDEFINED而不是OBJECT。

1 个答案:

答案 0 :(得分:1)

也许使用承诺?

示例:

 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