nodeJs基本身份验证问题 [英] nodeJs basic authentication issue

查看:80
本文介绍了nodeJs基本身份验证问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在nodejs(javascript)中使用基本身份验证(用户名和密码)向外部API发出API请求时,我没有得到正确的响应

I'm getting no proper response while make an API request to external API using basic authentication (username and password) in nodejs (javascript)

我使用了以下代码,响应为未定义",不确定此处缺少什么.但是我可以使用邮递员工具发出请求,而没有任何问题.

I used the below code and the response is "undefined", not sure what is missing here. But I was able to make a request using postman tool without any issues.

const request = require('request')
const user = '*****';
const pass = '*****!';

const url = 'https://servicenow.com/api/table'
var options = {
    url: url,
    auth: {
        username: user,
        password: pass
    }
};
 
request.get(options, (err, res, body) => {
    if (err) { 
        return console.log(err); 
    }
    console.log(body.url);
    console.log(body.explanation);
});

响应:

undefined
undefined

推荐答案

如果您对邮递员的api权利很高,则可以基于照片来做到这一点

if your api right with postman you can do like this based on photo

  1. 发送一个请求
  2. 点击 code
  3. 选择 nodejs-请求
  4. 复制
  1. send a request
  2. click on code
  3. select nodejs- Request
  4. copy

这篇关于nodeJs基本身份验证问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆