GVKun编程网logo

如何将 Accept-Ranges 标头添加到 nodejs 中的响应

12

在本文中,我们将为您详细介绍如何将Accept-Ranges标头添加到nodejs中的响应的相关知识,此外,我们还会提供一些关于android–通过在shouldInterceptRequest中向W

在本文中,我们将为您详细介绍如何将 Accept-Ranges 标头添加到 nodejs 中的响应的相关知识,此外,我们还会提供一些关于android – 通过在shouldInterceptRequest中向WebResourceRequest的标头添加标头,将自定义标头添加到WebView不起作用、angularjs – 请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma、node.js – Cassandra nodejs:如何获得Keyspaces和表格?、node.js – http.request中的nodeJS最大标头大小的有用信息。

本文目录一览:

如何将 Accept-Ranges 标头添加到 nodejs 中的响应

如何将 Accept-Ranges 标头添加到 nodejs 中的响应

我认为您唯一需要做的就是在此处添加:

Visual Studio Code

在此处查看 res.set({ "Content-Disposition": 'attachment; filename="big_buck_bunny_720p_5mb.mp4"',"Content-Type": "application/octet-stream","Content-Length": totalLength,"Accept-Ranges": // add here the value your server accepts }); 文档: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Accept-Ranges

它的语法是这样的:

Accept-Ranges

android – 通过在shouldInterceptRequest中向WebResourceRequest的标头添加标头,将自定义标头添加到WebView不起作用

android – 通过在shouldInterceptRequest中向WebResourceRequest的标头添加标头,将自定义标头添加到WebView不起作用

我需要为WebView中的页面加载添加自定义标头,但它不起作用,标头未设置:

@Override
public WebResourceResponse shouldInterceptRequest (WebView view,  WebResourceRequest request)
{
    request.getRequestHeaders().add("MyHeader","MyValue"); 
    return super.shouldInterceptRequest(view, request);
}

我在这做错了什么?我在Android 5上运行.

我已经看到很多答案,说你必须自己做HTTP请求并返回WebResourceResponse.这是因为即使你像我一样修改标题,它们也会被忽略吗?

我还试图在Android源代码中找到该位置的调用
调用shouldInterceptRequest的位置在哪里,所以我可以看到它是如何工作的,但我找不到它.

解决方法:

我自己找到了答案,它就在docs:

If the return value is null, the WebView will continue to load the
resource as usual. Otherwise, the return response and data will be
used.

此外,一个简单的测试显示WebViewClient.shouldInterceptRequest的基本实现返回null.因此,WebView基本上继续像往常一样加载资源.

换句话说,我不能只为标头添加一个值并期望它被使用.
我实际上需要自己做请求并返回响应.

太糟糕了,没有办法只修改标头并让默认实现使用它.

我知道我可以通过调用带有头文件的loadUrl方法设置头文件,但如果我第一次加载本地页面然后加载在线页面,则不会使用头文件.

angularjs – 请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma

angularjs – 请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma

当我试图保留以下代码来禁用ajax的缓存时,我收到此错误

angularapp.config(['appConfig','$httpProvider',function (appConfig,$httpProvider) {

if (!$httpProvider.defaults.headers.get) {
    $httpProvider.defaults.headers.get = {};
}

//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon,26 Jul 1997 05:00:00 GMT';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

}]);

我在chrome中遇到如下错误:

请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma.

但是当我删除以下代码时,它的工作正常.

$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Mon,26 Jul 1997 05:00:00 GMT';
$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';

谁能告诉我可能是什么问题?

解决方法

如果您可以在服务器端配置接受这些标头,那就没关系.
否则,您应该删除$httpProvider.defaulsts中设置的标头.
检查以下代码:

var data = {}
var httpCoonfig = {
    headers: {'Pragma': undefined,'Cache-Control': undefined,'X-Requested-With': undefined,'If-Modified-Since': undefined}
};
$http.post('https://www.google.com/',data,httpCoonfig).then(function(response){
// console.log(response)
},function(response){
     console.log(response)
});

node.js – Cassandra nodejs:如何获得Keyspaces和表格?

node.js – Cassandra nodejs:如何获得Keyspaces和表格?

我试图从Cassandra集群中获取元数据,密钥空间和表.我使用以下node.js代码:

var express = require('express');
var bodyParser = require('body-parser');
var cassandra = require('cassandra-driver');

var client = new cassandra.Client( { contactPoints : [ '127.0.0.1' ] } );
client.connect(function(err,result) {
    console.log('Connected To the Database.');
});

var app = express();
app.use(bodyParser.json());
app.set('json spaces',2);

app.get('/Metadata',function(req,res) {
    res.send(client.hosts.slice(0).map(function (node) {
        return { address : node.address,rack : node.rack,datacenter :     node.datacenter }
    }));
});



app.get('/Metadata/keyspaces',res) {
    client.execute("DESCRIBE KEYSPACES;",function(err,result) {
        if (err) {
            res.status(404).send(err);
        } else {
            res.json(result);        }
    });
});

app.get('/Metadata/tables',res) {
    client.execute("DESCRIBE TABLES",result) {
        if (err) {
            res.status(404).send({ msg : 'No Table found' });
        } else {
            res.json(result);        }
    });
});

var server = app.listen(3000,function() {
    console.log('Listening on port %d',server.address().port);
});

问题是,当我运行上面的代码时,我没有从数据库中获取现有的键空间或表,而是出现以下错误:

{
  "name": "ResponseError","message": "line 1:0 no viable alternative at input 'DESCRIBE'","info": "Represents an error message from the server","code": 8192,"query": "DESCRIBE KEYSPACES;"
}

我怎样才能看到这些信息?提前感谢您的帮助.

更新:

我刚才知道,DESCRIBE是一个cqlsh命令,所以我怎么能在我的情况下解决?

解决方法

DESCRIBE是仅在cqlsh工具上可用的命令.

尝试查询系统表,如here所述.

node.js – http.request中的nodeJS最大标头大小

node.js – http.request中的nodeJS最大标头大小

对于nodeJS v0.10.28,http请求中的标头内容的大小/长度是否有限制?

让我解释:

我需要使用第三方提供商提供的休息服务.返回给我的数据在请求的标题中,正文大部分是空的(120左右的字符).标头中的数据量从几个字符到几百字节不等.

var https = require('https');

var httpHeaders = {
    Authorization: 'Basic ' + new Buffer(user + ':' + psw).toString('base64'),accept: '*/*','Content-Type': 'text/plain; charset=utf-8'
};
var options = {
    host: "www.website.com",port: 8080,path: "/",method: 'GET',headers: httpHeaders,rejectUnauthorized: false,requestCert: true,agent: false
};

https.request(options,function(res) {
    res.setEncoding('utf8');
    if (res.statusCode == 200) {
        var json = res.headers["someHeaderInfo"];
        callback(null,{ "result" : JSON.parse(json) });
    } else {
        callback({ "error" : res.statusCode  });                            
    }
}).on('data',function (chunk) {
    console.log('BODY: ' + chunk);
}).on('error',function(e,res) {
    console.log("  Got error: " + e.message);
    callback({ "error" : e.message });
}).end();

上面的代码适用于较小尺寸的标题,但在on(‘error’,在较大的标题上带有“Parse Error”消息时失败.

删除on error子句会引发此异常:

Error: Parse Error
    at CleartextStream.socketonData (http.js:1583:20)
    at CleartextStream.read [as _read] (tls.js:511:12)
    at CleartextStream.Readable.read (_stream_readable.js:320:10)
    at EncryptedStream.write [as _write] (tls.js:366:25)
    at doWrite (_stream_writable.js:226:10)
    at writeOrBuffer (_stream_writable.js:216:5)
    at EncryptedStream.Writable.write (_stream_writable.js:183:11)
    at write (_stream_readable.js:582:24)
    at flow (_stream_readable.js:591:7)
    at Socket.pipeOnReadable (_stream_readable.js:623:5)

标题大小是否有限制,我可以改变吗?我有什么解决方案?

谢谢

解决方法

Node使用的HTTP协议解析器似乎是硬编码的,最大标头大小为80KB. Relevant constant.由于这是一个编译时常量,你必须使用自定义编译的Node版本来设置更大的常量.

听起来你正在使用的服务通过将大量数据放入标题中而犯了错误.标头用于有关请求正文的元数据.如果他们要返回那么多数据,他们应该将它包含在请求体中.

您可以使用像http-parser-js这样的备用HTTP解析器进行探索,因为它似乎没有限制.

我们今天的关于如何将 Accept-Ranges 标头添加到 nodejs 中的响应的分享已经告一段落,感谢您的关注,如果您想了解更多关于android – 通过在shouldInterceptRequest中向WebResourceRequest的标头添加标头,将自定义标头添加到WebView不起作用、angularjs – 请求标头字段预检响应中的Access-Control-Allow-Headers不允许使用Pragma、node.js – Cassandra nodejs:如何获得Keyspaces和表格?、node.js – http.request中的nodeJS最大标头大小的相关信息,请在本站查询。

本文标签: