我正在实现一个REST API,它涉及在服务器上创建一个对象.对象创建涉及多个步骤,可能需要一段时间.我不希望用户等待它.我只返回202响应,其中包含客户端请求的唯一请求ID,并在服务器上启动一些线程来创建对象.客户应该在将来检查请求是否完成.流程如下:
/my-app/<reqId>/my-app/<reqId>现在,在第三步,这些事情可能会发生:
现在我的API应该/my-app/<reqId>响应上述三种情况的http代码?
据我所知Vary: Accept-language,在多语言网站上工作时,应该设置响应头。在我的情况下,用户被重定向到特定语言的页面,例如http://www.example.com/nl/。
但是我找不到任何使用此Vary标头的网站。这是为什么?即使是来自Apple,Microsoft,Samsung,Facebook的多语言网站也不会使用此标头。
到目前为止,我还没有找到我的问题的答案,所以希望大家知道答案。我正在寻找一种在React中设置HTTP标头的简单方法。我认为将它们直接设置在路由器内部将是一个不错的解决方案:
render((
<Router history={browserHistory}>
<Route path="/" component={App}>
<Route path="about" component={About}/>
<Route path="users" component={Users}>
<Route path="/user/:userId" component={User}/>
</Route>
<Route path="*" component={NoMatch}/>
</Route>
</Router>
), document.getElementById('root'))
Run Code Online (Sandbox Code Playgroud)
可能作为属性或作为额外的标记<Header>,因此您可以轻松地为每个路由指定要设置的标头。例如,对于所有路线(path="*"):
内容安全政策:script-src'self'https: //apis.google.com
只是一个主意。是否有支持这种功能的模块?或如果不是这样,是否有任何原因呢?
我正在尝试从外部API(来自Mashape)获取一些数据,这些数据需要特定的标头来设置API密钥.
使用jQuery一切都很好:
$.ajax({
url: 'https://omgvamp-hearthstone-v1.p.mashape.com/cardbacks',
type: 'GET',
data: {},
dataType: 'json',
success: function(data) { console.dir((data.source)); },
error: function(err) { alert(err); },
beforeSend: function(xhr) {
xhr.setRequestHeader("X-Mashape-Authorization", "MY_API_KEY");
}
});Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用axios为反应应用程序执行相同的请求时,我遇到404错误:
axios.get({
url: 'https://omgvamp-hearthstone-v1.p.mashape.com/cardbacks',
headers: {
"X-Mashape-Authorization": "MY_API_KEY"
}
})Run Code Online (Sandbox Code Playgroud)
有什么我想念的吗?谢谢.
是否可以将标头(在.htaccess文件中定义)添加到PHP生成的响应中?
我有以下.htaccess文件,应该TestHeader为我的Apache Webserver提供的每个响应添加一个Header :
#<IfModule mod_headers.c>
# Header unset X-Powered-By
Header add TestHeader "It works."
#</IfModule>
Run Code Online (Sandbox Code Playgroud)
我在该文件夹中还有三个附加文件:
html.html
<html>content</html>
Run Code Online (Sandbox Code Playgroud)1.php
<?php
echo "<html>content php</html>";
Run Code Online (Sandbox Code Playgroud)2.php
<?php
header("TestHeader: Sent from PHP.");
echo "<html>content php</html>";
Run Code Online (Sandbox Code Playgroud)
html.html返回标头TestHeader: "It works."1.php并没有返回头TestHeader2.php返回标头TestHeader: "Sent from PHP."是否有可能使用.htaccess指令从PHP输出操作响应头?
编辑:PHP FastCGI在服务器上运行.
我成功生成了一个签名的URL,然后可以在有限的时间内使用该URL从s3存储桶下载资源。但是,我试图在params中使用ResponseContentDisposition属性,如此处所述:
我不确定我是否做错了,但是由于某些原因没有设置标题。例如,如果我使用url,则从s3.getSignedUrl中获取:
curl -i "https://foo-dev.s3.amazonaws.com/images/foo.jpg?AWSAccessKeyId=AKIAICBHUC26S6B446PQ&Expires=1468359314&Signature=EeBqx1G83oeusarBl2KUbbCCBgA%3D&response-content-disposition=attachment%3B%20filename%3Ddata.jpg"
标头是:
x-amz-id-2: SG9rjYQCcuqgKfjBmMbDQC2CNLcnqBAFzP7zINa99VYUwNijPOm5Ea/5fllZ6cnt/Qti7e26hbE=
x-amz-request-id: 2670068008525B1D
Date: Tue, 12 Jul 2016 21:26:16 GMT
Content-Disposition: inline; filename=foo.jpg
Last-Modified: Tue, 12 Jul 2016 00:47:23 GMT
ETag: "2a8e36651b24769170f4faa429f40f54"
Accept-Ranges: bytes
Content-Type: image/jpeg
Content-Length: 43373
Server: AmazonS3
Run Code Online (Sandbox Code Playgroud)
我使用这样的javascript s3 sdk进行设置:
function tempRedirect(req, res) {
var filename = req.params[0];
var contentDisposition = 'attachment; filename=data.jpg';
var params = {
Bucket: S3_BUCKET,
ResponseContentDisposition: contentDisposition,
Key: checkTrailingSlash(getFileKeyDir(req)) + filename
};
var s3 = new aws.S3(s3Options);
s3.getSignedUrl('getObject', params, function(err, url) {
res.redirect(url);
}); …Run Code Online (Sandbox Code Playgroud) 我有以下代码:
$.ajax({
url: "http://localhost:15797/api/values",
type: 'get',
contentType: 'application/json',
headers: {
"Authorization": "Bearer " + token
}
})
Run Code Online (Sandbox Code Playgroud)
工作正常,但我想在不使用Ajax的情况下做到这一点,我想要这样的东西:
$.ajax({
url: "http://localhost:15797/api/values",
type: 'get',
contentType: 'application/json',
headers: {
"Authorization": "Bearer " + token
}
})
Run Code Online (Sandbox Code Playgroud)
可能吗?或者只是在没有XMLHttpRequest的情况下做类似的事情?怎么样?
在开发中,我有一个在标准端口35729上运行的livereload服务器,但是由于我的策略具有,因此未加载该服务器script-src 'self'。有没有办法允许所有端口上的“自我”?
'localhost:*' 这也不是一个很好的解决方案,因为有时我会在本地网络上测试该站点,因此它可能是一个IP地址而不是“ localhost”。
当然,如果需要的话,我可以从开发中删除此标头,但我试图使其尽可能接近实际。
假设来自服务器的HTML文件包含以下标题:
Content-Type: application/octet-stream
Content-Disposition: attachment;
Run Code Online (Sandbox Code Playgroud)
我想改变Web扩展中的标题(以便HTML文件显示为常规网页):
Content-Type: text/html; charset=utf-8
(Removed Content-Disposition)
Run Code Online (Sandbox Code Playgroud)
在Google Chrome中,网络扩展程序成功更改标题,HTML文件显示为普通网页.在Firefox(48)中,虽然标题已更改,但系统会提示我下载HTML文件.我做错了什么或这是一个已知的错误?
请求示例:localhost:8000/test.html
代码如下.
Web扩展 - manifest.json:
{
"description": "Change content type",
"manifest_version": 2,
"name": "change-content-type",
"version": "1.0",
"permissions": [
"webRequest", "webRequestBlocking", "http://localhost/*"
],
"background": {
"scripts": ["background.js"]
}
}
Run Code Online (Sandbox Code Playgroud)
Web扩展 - Background.js如下所示:
function setHeader(headers, name, value) {
for (var header of headers) {
if (header.name.toLowerCase() == name.toLowerCase()) {
header.value = value;
return;
}
}
headers.push({ name : name, value : value });
}
function removeHeader(headers, …Run Code Online (Sandbox Code Playgroud) firefox http http-headers google-chrome-extension firefox-addon-webextensions
和往常一样,它永远对我不起作用...
我尝试流式传输视频,并且下面的代码做得很好:
function send_back_video($video_path)
{
ob_clean();
$mime = "application/octet-stream"; // The MIME type of the file, this should be replaced with your own.
$size = filesize($video_path); // The size of the file
// Send the content type header
header('Content-type: ' . $mime);
// Check if it's a HTTP range request
if(isset($_SERVER['HTTP_RANGE']))
{
// Parse the range header to get the byte offset
$ranges = array_map(
'intval', // Parse the parts into integer
explode(
'-', // The range separator
substr($_SERVER['HTTP_RANGE'], 6) …Run Code Online (Sandbox Code Playgroud) http-headers ×10
http ×3
.htaccess ×1
amazon-s3 ×1
apache ×1
api ×1
axios ×1
firefox ×1
firefox-addon-webextensions ×1
html ×1
html5-video ×1
javascript ×1
multilingual ×1
node.js ×1
php ×1
react-router ×1
rest ×1
token ×1