【问题标题】:How to get a token for Rest Client?如何获取 Rest Client 的令牌?
【发布时间】:2020-03-27 16:12:32
【问题描述】:

当我授权时,它没有错误地通过,但我无法获得进一步请求(获取、发布)的令牌。

结果,我应该得到这样一个令牌:

{ "token": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjoxLCJyb2xlX25hbWUiOiLQkNC00LzQuNC90LjRgdGC0YDQsNGC0L7RgCIsImlhdCI6MTU4NTI5MDY5MSwiZXhwIjoxNTg1Mjk0MjkxfQ.saODDtA27NAf6hThUhvltfv6bpeieWDQ8bYxLhjq6Dc" }

但我没有在对象中看到他。

结果:

Request {
    maxAttempts: 4,
    retryDelay: 5000,
    fullResponse: true,
    attempts: 1,
    options: {
      url: 'http://localhost:3000/api/auth/login',
      method: 'POST',
      json: { username: 'admin', password: '123456' },
      maxAttempts: 5,
      retryDelay: 5000,
      fullResponse: true,
      promiseFactory: [Function: defaultPromiseFactory]
    },
    retryStrategy: [Function: HTTPError],
    delayStrategy: [Function],
    _timeout: null,
    _req: Request {
      _events: [Object: null prototype] {
        error: [Function: bound ],
        complete: [Function: bound ],
        pipe: [Function]
      },
      _eventsCount: 3,
      _maxListeners: undefined,
      method: 'POST',
      maxAttempts: 5,
      retryDelay: 5000,
      fullResponse: true,
      promiseFactory: [Function: defaultPromiseFactory],
      callback: [Function],
      readable: true,
      writable: true,
      explicitMethod: true,
      _qs: Querystring {
        request: [Circular],
        lib: [Object],
        useQuerystring: undefined,
        parseOptions: {},
        stringifyOptions: {}
      },
      _auth: Auth {
        request: [Circular],
        hasAuth: false,
        sentAuth: false,
        bearerToken: null,
        user: null,
        pass: null
      },
      _oauth: OAuth { request: [Circular], params: null },
      _multipart: Multipart {
        request: [Circular],
        boundary: 'd1a5b314-42ec-44c0-87ab-c4ebf0a28613',
        chunked: false,
        body: null
      },
      _redirect: Redirect {
        request: [Circular],
        followRedirect: true,
        followRedirects: true,
        followAllRedirects: false,
        followOriginalHttpMethod: false,
        allowRedirect: [Function],
        maxRedirects: 10,
        redirects: [],
        redirectsFollowed: 0,
        removeRefererHeader: false
      },
      _tunnel: Tunnel {
        request: [Circular],
        proxyHeaderWhiteList: [Array],
        proxyHeaderExclusiveList: []
      },
      headers: {
        host: 'localhost:3000',
        accept: 'application/json',
        'content-type': 'application/json',
        'content-length': 40
      },
      setHeader: [Function],
      hasHeader: [Function],
      getHeader: [Function],
      removeHeader: [Function],
      localAddress: undefined,
      pool: {},
      dests: [],
      __isRequestRequest: true,
      _callback: [Function: bound ] AsyncFunction,
      uri: Url {
        protocol: 'http:',
        slashes: true,
        auth: null,
        host: 'localhost:3000',
        port: '3000',
        hostname: 'localhost',
        hash: null,
        search: null,
        query: null,
        pathname: '/api/auth/login',
        path: '/api/auth/login',
        href: 'http://localhost:3000/api/auth/login'
      },
      proxy: null,
      tunnel: false,
      setHost: true,
      originalCookieHeader: undefined,
      _disableCookies: true,
      _jar: undefined,
      port: '3000',
      host: 'localhost',
      path: '/api/auth/login',
      _json: true,
      body: '{"username":"admin","password":"123456"}',
      httpModule: {
        _connectionListener: [Function: connectionListener],
        METHODS: [Array],
        STATUS_CODES: [Object],
        Agent: [Function],
        ClientRequest: [Function: ClientRequest],
        IncomingMessage: [Function: IncomingMessage],
        OutgoingMessage: [Function: OutgoingMessage],
        Server: [Function: Server],
        ServerResponse: [Function: ServerResponse],
        createServer: [Function: createServer],
        get: [Function: get],
        request: [Function: request],
        maxHeaderSize: [Getter],
        globalAgent: [Getter/Setter]
      },
      agentClass: [Function: Agent] { defaultMaxSockets: Infinity },
      agent: Agent {
        _events: [Object: null prototype],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 80,
        protocol: 'http:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: false,
        maxSockets: Infinity,
        maxFreeSockets: 256
      }
    },
    _callback: null,
    _resolve: [Function: promiseResolve],
    _reject: [Function: promiseReject],
    _promise: Promise {
      _handler: Pending {
        consumers: undefined,
        receiver: undefined,
        handler: undefined,
        resolved: false
      }
    },
    reply: [Function: requestRetryReply]
  }

请求:

const auth =  {
    username    : "admin",
    password : "123456"
}

var options = {
    url: `${config.url}/api/auth/login`,
    method: 'POST',
    json: auth
};
try {
    var result =  request(options);
    console.log(result)

} catch {
    console.log('[ERROR]:', err);
}

【问题讨论】:

    标签: node.js express request jwt


    【解决方案1】:

    你没有忘记回调吗?我的意思是:

    const auth =  {
        username    : "admin",
        password : "123456"
    }
    
    var options = {
        url: `${config.url}/api/auth/login`,
        method: 'POST',
        json: auth
    };
    
    request(options, function(err, res, body) {
        if(err){
            console.log('[ERROR]:', err);
        }
        else{
            let result = JSON.parse(body);
            console.log(result);
        }
    });
    ...
    

    【讨论】:

      猜你喜欢
      • 2017-01-15
      • 2015-05-19
      • 2018-11-26
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      • 2015-09-06
      • 1970-01-01
      • 2019-11-08
      相关资源
      最近更新 更多