Rub :: Net :: HTTP를 사용하여 HTTP 요청을 만들고 있는데 모든 응답 헤더를 얻는 방법을 알 수 없습니다.Ruby HTTP 요청에서 응답 헤더 가져 오기
나는 response.header
과 response.headers
을 시도했지만 아무런 효과가 없습니다.
Rub :: Net :: HTTP를 사용하여 HTTP 요청을 만들고 있는데 모든 응답 헤더를 얻는 방법을 알 수 없습니다.Ruby HTTP 요청에서 응답 헤더 가져 오기
나는 response.header
과 response.headers
을 시도했지만 아무런 효과가 없습니다.
응답 개체에는 실제로 머리글이 들어 있습니다.
자세한 내용은 "Net::HTTPResponse"을 참조하십시오.
당신은 할 수 있습니다 : 당신은 또한 머리글을 반복하는 응답 객체에 each_header
또는 each
를 호출 할 수 있습니다
response['Cache-Control']
. 당신이 정말로 응답 객체의 외부 헤더를 원하는 경우
의 RestClient
라이브러리 response.headers
의 예상 행동을 가지고 response.to_hash
주를 호출합니다.
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"
}
응답 Net::HTTPResponse
아래로 열거를 반환합니다 당신이 @Intrepidd 말했다으로 each_header
방법에서 얻을 수있는 Net::HTTPHeader
에서 헤더가 포함되어
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"]
]
당신은 to_h
방법을 사용하여 실제 해시를 얻을 수 있습니다 아래와 같이 :
response.each_header.to_h
{
"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"
}
Net/HTTP는 악명 높은 API를 가지고 있습니다. httpclient와 같은 다른 서버를 사용하고 있다면'response.header'는 방금 작업했을 것입니다. –
나쁜 뜻은 무엇입니까? 왜 나는 그것을 피하고 싶습니까? – BlackHatSamurai
라이브러리 자체가 나쁘지는 않지만 발견 한 것처럼 API가 복잡하고 직관적이지 않습니다. 내가 할 수있을 때 항상 HTTPclient 또는 HTTParty, Rest-Client 등과 같은 더 많은 기능을 가진 래퍼 라이브러리를 사용한다. –