【问题标题】:Not getting response with http GET call in prortactor在量角器中没有通过 http GET 调用得到响应
【发布时间】:2022-01-12 06:44:35
【问题描述】:

我在我的自动化套件中调用 GET api,但没有得到响应。邮递员使用相同的 API。我有很多 console.log 没有被打印出来。也没有错误。下面是sn-p。


        var deferred = protractor.promise.defer();
        var options = {
            method: "GET",
            hostname: hostname,
            port: 8080,
            path: '/v1/fis/'+fiId+'/businessCustomers/'+userAuthId+'/approvals',
            headers: {
                'authorization': 'testclient',
                'content-type': 'application/json',
                'accept': 'application/json',
            }
        };
    
        var req = http.request(options, function (response) {
            browser.sleep(1000);
            console.log('headers are:', options);
            console.log('hostname is: ', hostname);
            browser.sleep(1000);
            console.log('STATUS for approval API : ' + res.statusCode);
            var chunks = [];

            response.on("data", function (chunk) {
                console.log('in chunk block::');
                chunks.push(chunk);
            });

            response.on("end", function () {
                var body = Buffer.concat(chunks);
                var finaljson = JSON.parse(body.toString());
                console.log('finaljson is:****', finaljson);
                successCallBack(finaljson.SuccessResponse);
            });
        });

        req.end();
        return deferred.promise;

    };```


Any insights will be helpful. TIA!

【问题讨论】:

    标签: javascript node.js automation protractor


    【解决方案1】:

    改用这样的方法。应该工作得更好。为简单起见,我省略了您的 console.log 消息,但您可以轻松地将它们添加到您需要的位置。

    function myFunction() {
      return new Promise(resolve, reject) {
        const options = {
            method: "GET",
            hostname: hostname,
            port: 8080,
            path: '/v1/fis/'+fiId+'/businessCustomers/'+userAuthId+'/approvals',
            headers: {
                'authorization': 'testclient',
                'content-type': 'application/json',
                'accept': 'application/json',
            }
        };
    
        http.get(options, (res) => {
          let rawData = '';
          res.on('data', (chunk) => { rawData += chunk; });
          res.on('end', () => {
            try {
              const finaljson = JSON.parse(rawData);
              resolve(finaljson.SuccessResponse);
            } catch (e) {
              reject(e.message);
            }
          });
    
        }).on('error', (e) => {
          reject(e);
        });
      }
    }
    

    然后您只需使用 myFunction().then((successResponse) => { // do stuff }) 调用它

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2015-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多