HTTPの参考書を読んでいるのですが
レスポンスメッセージの例が少ないので
Rubyでレスポンスメッセージを取得したいです。
Ruby
1require 'net/http' 2require 'uri' 3 4response = Net::HTTP.get_response(URI.parse("http://www.example.com/index.html")) 5puts response.body 6 7``` 8↓ 9```cmd 10<!doctype html> 11<html> 12<head> 13 <title>Example Domain</title> 14 15 <meta charset="utf-8" /> 16 <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> 17 <meta name="viewport" content="width=device-width, initial-scale=1" /> 18 <style type="text/css"> 19 body { 20 background-color: #f0f0f2; 21 margin: 0; 22 padding: 0; 23 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; 24 25 } 26 div { 27 width: 600px; 28 margin: 5em auto; 29 padding: 50px; 30 background-color: #fff; 31 border-radius: 1em; 32 } 33 a:link, a:visited { 34 color: #38488f; 35 text-decoration: none; 36 } 37 @media (max-width: 700px) { 38 body { 39 background-color: #fff; 40 } 41 div { 42 width: auto; 43 margin: 0 auto; 44 border-radius: 0; 45 padding: 1em; 46 } 47 } 48 </style> 49</head> 50 51<body> 52<div> 53 <h1>Example Domain</h1> 54 <p>This domain is established to be used for illustrative examples in documents. You may use this 55 domain in examples without prior coordination or asking for permission.</p> 56 <p><a href="http://www.iana.org/domains/example">More information...</a></p> 57</div> 58</body> 59</html>
エンティティボディの取得は割と少ないコードで行けたのですが
どうやったら**メッセージヘッダー全体(レスポンス)**を取得できるのでしょうか?
イメージとしてはこんな感じです。
ステータスコードだけなら取得できました。
ruby
1require 'net/http' 2require 'uri' 3 4response = Net::HTTP.get_response(URI.parse("http://www.example.com/index.html")) 5puts response.code 6
参考になるリンクまたはやり方を教えてください。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/21 03:46