我正在使用Net :: HTTP向Ruby发出HTTP请求,我无法弄清楚如何获取所有响应头。
我尝试了response.header
和response.headers
,但没有任何效果。
答案 0 :(得分:46)
响应对象实际上包含标题。
有关详细信息,请参阅“Net::HTTPResponse”。
你可以这样做:
response['Cache-Control']
您还可以在响应对象上调用each_header
或each
来迭代标题。
如果您确实需要响应对象之外的标头,请致电response.to_hash
答案 1 :(得分:3)
请注意,RestClient
库具有response.headers
的预期行为。
response.headers
{
:server => "nginx/1.4.7",
:date => "Sat, 08 Nov 2014 19:44:58 GMT",
:content_type => "application/json",
:content_length => "303",
:connection => "keep-alive",
:content_disposition => "inline",
:access_control_allow_origin => "*",
:access_control_max_age => "600",
:access_control_allow_methods => "GET, POST, PUT, DELETE, OPTIONS",
:access_control_allow_headers => "Content-Type, x-requested-with"
}
答案 2 :(得分:3)
回复DragonFly/x86_64 (Amnesiac) (ttyv0)\u001b[38;74H\u001b[28X\u001b[39dlogin:\u001b[74G\u001b(B\u001b[m\u001b[39;49m\u001b[37m\u001b[40m\u001b[15;36r\u001b[36;1H\u001b[7S\u001b[1;54r\u001b[31;74H\u001b[37m\u001b[40mFri Feb 16 05:29:19 UTC 2018\u001b[32;74HWelcome to DragonFly!\u001b[34;74HTo start the installer, login as 'installer'. To just get a shell prompt,\u001b[35;74Hlogin as 'root'.\u001b[39;81H\u001b[30m\u001b[47m \u001b[39;74H\u001b(B\u001b[m\u001b[39;49m\u001b[37m\u001b[40m" (spawn_id exp6) match glob pattern "login: "? no
包含来自Net::HTTPResponse
的标题,您可以通过@Intrepidd从@ {1}}方式获取该标题,该标题将返回如下的枚举数:
Net::HTTPHeader
您可以使用each_header
方法获取实际哈希,如下所示:
response.each_header
#<Enumerator: #<Net::HTTPOK 200 OK readbody=true>:each_header>
[
["x-frame-options", "SAMEORIGIN"],
["x-xss-protection", "1; mode=block"],
["x-content-type-options", "nosniff"],
["content-type", "application/json; charset=utf-8"],
["etag", "W/\"51a4b917285f7e77dcc1a68693fcee95\""],
["cache-control", "max-age=0, private, must-revalidate"],
["x-request-id", "59943e47-5828-457d-a6da-dbac37a20729"],
["x-runtime", "0.162359"],
["connection", "close"],
["transfer-encoding", "chunked"]
]
答案 3 :(得分:2)
如果您需要用户友好输出,则可以使用<?xml version="1.0" encoding="utf-8" ?>
<NavigationPage
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TransparentNavBarXForms.CustomNavigationPage"
BarTextColor="White">
<NavigationPage.BarBackgroundColor>
<OnPlatform x:TypeArguments="Color">
<On Platform="Android, iOS" Value="Transparent" />
</OnPlatform>
</NavigationPage.BarBackgroundColor>
</NavigationPage>
public partial class CustomNavigationPage : NavigationPage
{
public CustomNavigationPage() : base()
{
InitializeComponent();
}
public CustomNavigationPage(Page root) : base(root)
{
InitializeComponent();
}
public bool IgnoreLayoutChange { get; set; } = false;
protected override void OnSizeAllocated(double width, double height)
{
if (!IgnoreLayoutChange)
base.OnSizeAllocated(width, height);
}
}
public class CustomNavigationPageRenderer : NavigationPageRenderer
{
public CustomNavigationPageRenderer(Context context) : base(context)
{
}
IPageController PageController => Element as IPageController;
CustomNavigationPage CustomNavigationPage => Element as CustomNavigationPage;
protected override void OnLayout(bool changed, int l, int t, int r, int b)
{
CustomNavigationPage.IgnoreLayoutChange = true;
base.OnLayout(changed, l, t, r, b);
CustomNavigationPage.IgnoreLayoutChange = false;
int containerHeight = b - t;
PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));
for (var i = 0; i < ChildCount; i++)
{
AView child = GetChildAt(i);
if (child is Android.Support.V7.Widget.Toolbar)
{
continue;
}
child.Layout(0, 0, r, b);
}
}
}
public partial class App : Application
{
public App()
{
InitializeComponent();
MainPage = new CustomNavigationPage(new MainPage());
}
}
:
each_capitalized
这将打印:
response.each_capitalized { |key, value| puts " - #{key}: #{value}" }
答案 4 :(得分:0)
将其存储在哈希中=>
response_headers = {}
your_object.response.each { |key, value| response_headers.merge!(key.to_s => value.to_s) }
puts response_headers
答案 5 :(得分:0)
使用HTTParty作为替代方案也可以轻松实现:
HTTParty.get(uri).headers