2010-01-24 41 views
25

我是來自Java的Ruby的新手。我試圖做一個HTTP GET請求,我得到一個400的HTTP響應代碼。我通過HTTP調用的服務非常特別,我很確定我的請求不完全正確。在我執行頭部請求(下面)後仔細檢查req對象,以便仔細檢查發送的request_headers是我認爲正在發送的內容,這會很有幫助。有沒有辦法打印出req對象?使用set_debug_output#<Net::HTTP www.blah.com:443 open=false>如何打印有關NET:HTTPRequest的信息以進行調試?

回答

49

req = Net::HTTP.new(url.host, url.port) 
req.use_ssl = true 

res = req.head(pathWithScope, request_headers) 

code = res.code.to_i 
puts "Response code: #{code}" 

我嘗試這樣做:puts "Request Debug: #{req.inspect}"但只打印此。

http = Net::HTTP.new(url.host, url.port) 
http.set_debug_output($stdout) # Logger.new("foo.log") works too 

那和更http://github.com/augustl/net-http-cheat-sheet :)

+0

真棒,謝謝。 – Upgradingdave 2010-01-24 20:51:13

+0

當我在POST請求中使用'set_debug_output'時,它不記錄請求主體。其他一切都很好(請求標題,響應正文,標題)。任何想法如何使它記錄請求正文? – tikh 2017-09-12 22:27:08