简体   繁体   English

基本身份验证在node-rest-client节点中不起作用

[英]Basic authentication not working in node-rest-client node

I'm using node-rest-client library to call my rest API running a python flask app in a test server. 我正在使用node-rest-client库调用在测试服务器中运行python flask应用程序的rest API。 The Node setup and request code is below. 节点设置和请求代码如下。

If I add a user token to the request header it words fine, but in the call to obtain the user token using basic auth my python system is successfully authenticating and returning the token with a 200 status, but the flask server is then changing this to a 400 Bad Request. 如果我将用户令牌添加到请求标头中,就可以了,但是在使用基本身份验证获取用户令牌的调用中,我的python系统已成功进行身份验证并返回状态为200的令牌,但flask服务器随后将其更改为400错误的请求。 It works fine making the call using Postman. 使用Postman拨打电话可以正常工作。

Is there something missing from my two config objects for node-rest-client? 我的两个用于node-rest-client的配置对象中缺少什么吗?

Cheers. 干杯。

var options_auth = { 
    user: "bob", 
    password: "password",
    mimetypes: {
        json: ["application/json", "application/json;charset=utf-8"]
    } 
};

var Client = require('node-rest-client').Client;
var client = new Client(options_auth);
var method = "/authtoken";

var args = {
    headers: { 
        "Content-Type": "application/json",
        "api-key": "asf89a7assa98d7sd98d98ds",
        //"token": "dsf98s7dsf98dsf7sd98f7dsf",
        "Accept": "application/json"
    },
    responseConfig: {
        timeout: 1000 //response timeout
    }
};

client.get(Api.url+method, args, function (data, response) {
    // parsed response body as js object 
    // raw response 
    //console.log(response);

    if(Buffer.isBuffer(data)){
        data = data.toString('utf8');
    }
    console.log(data);

    var stringData = data.toString('utf8');

    console.log("String data = "+stringData);

}).on('error', function (err) {
    console.error('Something went wrong with the http client', err);
});

Also, spotted these differences between the request headers received by the server: 另外,找出服务器接收到的请求标头之间的这些差异:

// Node Request fails: 400
  'headers': EnvironHeaders([
        ('Authorization', u'Basic XXXXXXXXXXXXXX=='),
        ('Vga-Api-Key', u'xxxxxxxxxxxxxxxxxxxxxxxx'),
        ('Content-Length', u'0'), 
        ('Connection', u'close'), 
        ('Host', u'127.0.0.1:8080'), 
        ('Accept', u'*/*'), 
        ('Content-Type', u'application/json')]),

  // Postman Request works: 200
  'headers': EnvironHeaders([
        ('Authorization', u'Basic XXXXXXXXXXXXXX=='), 
        ('Vga-Api-Key', u'xxxxxxxxxxxxxxxxxxxxxxxx'), 
   *    ('Content-Length', u''), 
   *    ('Connection', u'keep-alive'), 
        ('Host', u'127.0.0.1:8080'), 
   *    ('Cache-Control', u'no-cache'), 
        ('Accept', u'*/*'), 
        ('Content-Type', u''), 
   *    ('Accept-Encoding', u'gzip, deflate')]),

The problem is your setting of the header Content-Type: application/json and the probable calling in the server of request.get_json() directly, or indirectly via the (deprecated) request.json property. 问题是您设置了标题Content-Type: application/json以及直接或通过(不建议使用的) request.json属性间接在request.get_json()服务器中调用的可能性。

When get_json() is called Flask will check to see that a JSON payload has been sent in the body of the request and then parse it if present. 调用get_json() ,Flask将检查是否已在请求正文中发送了JSON有效负载,然后对其进行解析(如果存在)。 That's OK if the request actually contains JSON in the body, but, being a GET request, yours doesn't. 如果该请求实际上在主体中包含JSON,那是可以的,但是作为GET请求,您的请求就没有。 In this case, its JSON expectation being unfulfilled, the server raises a BadRequest error and returns a HTTP 400 error response. 在这种情况下,无法满足其JSON期望,服务器将引发BadRequest错误并返回HTTP 400错误响应。

From what you've shown your request doesn't need to be JSON because the authorisation username and password are passed in the Authorization: Basic xxxxxxxx header. 根据您显示的内容,您的请求不需要是JSON,因为授权用户名和密码在“ Authorization: Basic xxxxxxxx标头中传递。

The easiest and best way to fix the problem is to simply omit the content type header. 解决此问题的最简单,最佳方法是简单地省略内容类型标头。

Alternatively you can tell Flask not to complain if there is no JSON data to parse by passing silent=True to get_json , but this just papers over the problem and is not a good idea. 另外,您可以通过将silent=True传递给get_json来告诉Flask不要抱怨是否没有要解析的JSON数据,但这只是解决问题get_json ,并不是一个好主意。

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

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