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

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

最佳答案

也许使用诺言?

例:

 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

关于javascript - 将响应Request.post token 保存在变量中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58771061/

10-10 06:03