我正在使用set_headers方法修补Capybara :: Session,该方法分配给Capybara :: RackTest :: Browser的选项属性(我已经从attr_reader更改为attr_accessor).
补丁:
class Capybara::RackTest::Browser
attr_accessor :options
end
class Capybara::Session
def set_headers(headers)
if driver.browser.respond_to?(:options=) #because we've monkey patched it above
options = driver.browser.options
if options.nil? || options[:headers].nil?
options ||= {}
options[:headers] = headers
else
options[:headers].merge!(headers)
end
else
raise Capybara::NotSupportedByDriverError
end
end
end
Run Code Online (Sandbox Code Playgroud)
在我的请求规范中,我正在做:
page.set_headers("REMOTE_ADDR" => "1.2.3.4")
visit root_path
Run Code Online (Sandbox Code Playgroud)
这有效,但我想知道是否有更好的方法,只是能够在请求上设置自定义remote_ip/remote_addr似乎有点过分.有什么想法吗?
integration-testing rspec ruby-on-rails request-headers capybara
我正在尝试将我对webservices的所有调用移动到我刚刚创建的可移植类库(PCL)来组织和重用我的代码.我的目标框架是.NET for Windows Store应用程序; .NET Framework 4.5; Silverlight 4及更高版本和WP7及更高版本.
在我的Win RT项目中,我一直在通过实现命名空间System.ServiceModel.Dispatcher中提供的IClientMessageInspector接口来设置消息头.但在我的PCL项目中,该接口以及System.ServiceModel.Description.IEndpointBehavior不可用.
因此,我需要了解如何将消息头/服务头附加到来自具有这些目标框架的PCL项目的服务调用.任何人都有我应该尝试的经验和/或建议吗?
只是为了添加更多信息,我现在尝试创建一个WP8项目,并注意到这些接口也不可用.所以IClientMessageInspector和IEndpointBehavior可能不适用于我的PCL项目,因为它的目标是WP8,它本身就错过了它们.
我正在尝试对GroupMe API进行API调用以获取JSON响应,但一直收到以下错误:
XMLHttpRequest cannot load ...(call url)...
Request header field X-CSRFToken is not allowed by Access-Control-Allow-Headers in preflight response.
Run Code Online (Sandbox Code Playgroud)
我的Javascript看起来像这样:
var xmlhttp = new XMLHttpRequest();
var url = (call url)
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
xmlhttp.open("GET", url, true);
xmlhttp.setRequestHeader("Access-Control-Allow-Headers", "*");
xmlhttp.setRequestHeader('Access-Control-Allow-Origin', '*');
$.getJSON(url, function(data){
var array = data.response.messages.reverse();
for(i = 0; i<array.length; i++){
$('.messages').append("<div class='message'>"+array[i].name+":<br>"+array[i].text+"</div>");
}
});
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
Run Code Online (Sandbox Code Playgroud)
我真的不明白请求标头是如何工作的所以我猜我没有正确设置标头.有人能指出我如何设置标题来解决这个问题吗?
javascript xmlhttprequest access-control csrf request-headers
function createRequest(method) {
const init = {
method,
headers: new Headers({.....}),
};
return new Request(url, init); }
Run Code Online (Sandbox Code Playgroud)
我在上面的代码(https://davidwalsh.name/fetch)中使用Request标头(带有Fetch )
但是,在使用Jest编写单元测试用例时,它给了我这个错误:ReferenceError:Headers未定义
我是否需要模拟这些标准模块?如何在单元测试用例中导入标题
我有一个基本的SPA(反应)<-> API(网络核心2.2)设置,带有2个环境:dev和prod(小型项目)。API
侧面有一个身份验证机制,用于检查httponly
每个包含JWT的请求中cookie 的存在。
在开发环境中,它可以正常运行okey-dokey:,allowCredentials()
并且已withCredentials = true
在API和react应用程序中进行了设置。两者都在我的本地主机的不同端口上运行。
但是在生产环境中(单独的Heroku dynos),它不会设置httponly
cookie:我可以使用我的凭据登录,响应标题包含带有jwt的cookie,但是我要发出的其他每个请求都将不包含cookie。 cookie标头在请求标头中!
然后我得到一个401 Unauthorized ...
错误(这是逻辑上的)。我花了数小时尝试所有事情,这让我发疯。
我的简单身份验证XHR(原始)调用:
var request = new XMLHttpRequest()
request.open('POST', apiAuthenticateUser, true)
request.setRequestHeader('Content-type', 'application/json')
request.withCredentials = true
request.send(postData)
Run Code Online (Sandbox Code Playgroud)
我Startup.cs
在.net核心api中的配置:
var request = new XMLHttpRequest()
request.open('POST', apiAuthenticateUser, true)
request.setRequestHeader('Content-type', 'application/json')
request.withCredentials = true
request.send(postData)
Run Code Online (Sandbox Code Playgroud)
那就是我如何在api控制器动作响应中设置包含jwt的httponly cookie:
public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
IdentityModelEventSource.ShowPII = true;
} else {
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseCors(
options …
Run Code Online (Sandbox Code Playgroud) javascript request-headers response-headers cookie-httponly asp.net-core
我有以下代码,我将一个网页下载到一个字节数组,然后用Response.Write打印它:
WebClient client = new WebClient();
byte[] data = client.DownloadData(requestUri);
/*********** Init response headers ********/
WebHeaderCollection responseHeaders = client.ResponseHeaders;
for (int i = 0; i < responseHeaders.Count; i++)
{
Response.Headers.Add(responseHeaders.GetKey(i), responseHeaders[i]);
}
/***************************************************/
Run Code Online (Sandbox Code Playgroud)
除了响应头之外,我还需要添加请求头.我尝试使用以下代码:
/*********** Init request headers ********/
NameValueCollection requestHeaders = Request.Headers;
foreach (string key in requestHeaders)
{
client.Headers.Add(key, requestHeaders[key]);
}
/***************************************************/
Run Code Online (Sandbox Code Playgroud)
但它不起作用,我得到以下异常:
必须使用适当的property.Parameter name:name修改此标头
有人可以帮帮我吗?使用WebClient添加请求标头的正确方法是什么?
谢谢.
Cloudfront 正在将标头传递到我们的 vue 页面,我们想读取它们。这可能吗?目前为止在网上还没有找到任何东西。谢谢!
我使用以下命令将 zip 文件从 Nodejs 服务器发送到浏览器
res.set("Content-Type", "application/octet-stream");
res.set("Content-disposition", `attachment; filename="`+zip_name+`.zip"`);
res.set("Content-Length", zipBuff.length);
res.send(zipBuff);
Run Code Online (Sandbox Code Playgroud)
然后我使用以下方法获取它:
fetch("/my/url", {
method: "POST",
body: formData,
})
.then(response => {
return response.blob();
})
.then(response => {
const blob = new Blob([response], {type: 'application/zip'});
const downloadUrl = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = downloadUrl;
a.download = "blah.zip";
document.body.appendChild(a);
a.click();
});
Run Code Online (Sandbox Code Playgroud)
我希望能够使用zip_name
而不是blah
文件名,但我无法弄清楚如何Content-disposition
使用fetch
.
有人可以解释一下它是如何完成的吗?
我想拦截所有正在发送的 XHR 请求,并在发送请求之前更改它们的 URL 和标头。
发现了这个类似的问题,但那里没有答案。
我尝试了 hooking XMLHttpRequest.prototype.open
,但它只能让我访问响应:
(function () {
var origOpen = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function () {
console.log(arguments); // prints method ("GET"), URL
console.log(this); // prints response, responseText, responseURL, status, statusText, and onXXX handlers
origOpen.apply(this, arguments);
};
})();
Run Code Online (Sandbox Code Playgroud)
还尝试了 hooking XMLHttpRequest.prototype.setRequestHeader
,但它只能让我一一访问正在设置的每个标头值,而且我无法将其与请求的 URL 关联起来:
(function () {
var origSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader;
XMLHttpRequest.prototype.setRequestHeader = function (header, value) {
console.log("header", header);
console.log("value", value);
origSetRequestHeader.apply(this, arguments);
};
})();
Run Code Online (Sandbox Code Playgroud)
我设法挂钩XMLHttpRequest.prototype.send
设置自定义标头,但由于我想更改现有标头键,因此它会附加我的新值而不是替换现有值。其他人也遇到了同样的问题:1 , 2 :
(function …
Run Code Online (Sandbox Code Playgroud) javascript xmlhttprequest httprequest request-headers http-headers
我没有成功地使用用户代理切换器和管理器扩展来渲染具有移动友好界面的 Web 应用程序。经过进一步挖掘,我注意到尽管浏览器通过用户代理字符串将自身标识为移动设备,但sec-ua-*标头仍然存在并将正确的平台泄漏到服务器:
sec-ch-ua:“不是;A 品牌”;v =“99”,“Google Chrome”;v =“97”,“Chromium”;v =“97” sec
-ch-ua-mobile:?0
秒-ch-ua-平台:“Windows”
基于此,我假设应用程序将应用程序的桌面版本返回给用户。
有没有什么方法可以抑制浏览器在其请求中发送这些标头(除了使用旧浏览器)?使用 --disable-features=UserAgentClientHint启动 Chrome确实禁用了这些标头。
request-headers ×10
javascript ×5
fetch-api ×2
http-headers ×2
asp.net-core ×1
c# ×1
capybara ×1
csrf ×1
es6-modules ×1
httprequest ×1
jestjs ×1
request ×1
rspec ×1
user-agent ×1
vue.js ×1
webclient ×1