简体   繁体   English

如何在NodeJS中调用登录API并在命令提示符下获取输出?

[英]How to call login API and get output on command prompt in nodeJS?

In this code i am calling login API. 在此代码中,我正在调用登录API。 and also using JSON to parse the data.when i am running this code on command prompt no output is there.what is wrong thing i am doing in my code and what is the right way to do that please help me. 并且还使用JSON解析数据。当我在命令提示符下运行此代码时,没有输出。我在代码中做错什么了,什么是正确的方法,请帮帮我。 --I supposed to see my output on command prompt-- -我应该在命令提示符下看到输出-

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    var querystring = require('querystring');
    var https = require('https');

    var host = 'https://blahabc.com/api/login.json';
    var username = 'xxxxx';
    var password = '******';
    var apiKey = '*****';
    var sessionId = '1234567890';


    function performRequest(endpoint, method, data, success) {
    console.log("hello");
      var dataString = JSON.stringify(data);
      var headers = {};

      if (method == 'POST') {
        endpoint += '?' + querystring.stringify(data);
      }
      else {
        headers = {
          'Content-Type': 'application/json',
          'Content-Length': dataString.length
        };
      }
      var options = {
        host: 'blahabc.com',
        path: '/api/login',
        method: 'POST',
      //  headers: headers
      };
    }
    console.log("bye");
    function login() {
      performRequest('/api/login', 'POST', {
        username: 'xxxxx',
        password: '******',
        api_key_id: '******'
      }, 
      function(data) {
        console.log(data);
        sessionId = data.result.id;
        console.log('Logged in:', sessionId);
        console.log("tring");
        getCards();
      });
      console.log("hello");
    }

    function getCards() {
      console.log("hello");
      performRequest('/api/post_login' + deckId + '/cards', 'POST', {
        session_id: '1234567890',
        "_items_per_page": 100
      }, function(data) {
        console.log('Fetched ' + data.result.paging.total_items + ' cards');
      });
    }
    login();

You aren't calling anything to make a request in your performRequest() function. 您不需要在performRequest()函数中调用任何东西来发出请求。 You need to make a call to XMLHttpRequest to post the request to the server. 您需要调用XMLHttpRequest才能将请求发布到服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM