![](/img/trans.png)
[英]How to print default HTTP GET/POST REQUEST/RESPONSE Headers and add new headers in request using net/http in Ruby on console
[英]can't get a response using Ruby's Net::HTTP with headers
這是我的代碼:
#!/usr/bin/env ruby
require 'net/http'
uri = URI.parse('https://api.lendingclub.com/api/investor/v1/loans/listing')
http = Net::HTTP.new(uri.host, uri.port)
request = Net::HTTP::Get.new(uri.request_uri)
request.initialize_http_header({'Authorization' => 'exampleQd0ddDKREKgdpeDOKsdfs3434aA='})
request.initialize_http_header({'Accept' => 'application/json'})
request.initialize_http_header({'Content-Type' => 'application/json'})
response = http.request(request)
這是我的結局:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:153:in `read_nonblock': end of file reached (EOFError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:153:in `rbuf_fill'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:134:in `readuntil'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/protocol.rb:144:in `readline'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http/response.rb:39:in `read_status_line'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http/response.rb:28:in `read_new'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1406:in `block in transport_request'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1369:in `block in request'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:852:in `start'
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/net/http.rb:1367:in `request'
from /Users/jeff/Documents/My Synched Cloud Files/Documents/My Scripts/Python/Development/Lending Club/ruby/LC_get_notes_new.rb:12:in `<main>'
不知道為什么所有錯誤都來自Frameworks / Ruby。 我只是想從文件(它是一個腳本)而不是網站的任何部分運行它。 如果您能告訴我如何在代碼的這一部分工作后將響應保存到文件中,則可以加分。 順便說一句,我在Python中一切正常,但是我試圖將其移植到Ruby(對我來說這是新的)。 謝謝!
這是一個使用POST的示例
require 'net/http'
http = Net::HTTP.new('nl3.forgeofempires.com')
path = '/game/json?h=c29e13c18d3'
data = [
%{5a6029226f[{"clientIdentification":{"requiredBackendVersion":"1.17","appType":null,"deviceId":null,"registrationId":null,"platform":"bro","androidDeviceId":null,"platformVersion":"web","clientVersionNumber":"1.17","__class__":"ClientIdentification"}, etc...}]}
]
headers = {
'Cookie' => cookie,
"Content-Length" => l,
'Connection' => 'keep-alive',
"Origin" => "http://cdn.nl.forgeofempires.com",
'Referer' => "http://cdn.nl.forgeofempires.com/swf/Preloader.swf?1389689898",
'Content-Type' => 'application/x-www-form-urlencoded'
}
resp = http.post(path, data, headers)
File.write('response.json', resp.body)
試試這個:
#!/usr/bin/env ruby
require 'net/http'
uri = URI.parse('https://api.lendingclub.com/api/investor/v1/loans/listing')
request = Net::HTTP::Get.new uri.request_uri
res = Net::HTTP.start(uri.host, uri.port,
:use_ssl => uri.scheme == 'https') {|http| http.request request}
request.initialize_http_header({'Authorization' => 'exampleQd0ddDKREKgdpeDOKsdfs3434aA='})
request.initialize_http_header({'Accept' => 'application/json'})
request.initialize_http_header({'Content-Type' => 'application/json'})
response = res.request(request)
如果要將響應寫入文件中
File.write('filename', response)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.