简体   繁体   English

不能使用带有标题的Ruby的Net :: HTTP得到响应

[英]can't get a response using Ruby's Net::HTTP with headers

Here's my code: 这是我的代码:

#!/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)

Here's where I end up: 这是我的结局:

/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>'

Not sure why all the errors are from Frameworks/Ruby. 不知道为什么所有错误都来自Frameworks / Ruby。 I am just trying to run this from a file (it's a script), not as any part of a website. 我只是想从文件(它是一个脚本)而不是网站的任何部分运行它。 Bonus points if you can then tell me how to save the response to a file once I get this part of the code working. 如果您能告诉我如何在代码的这一部分工作后将响应保存到文件中,则可以加分。 By the way, I have all this working fine in Python but I am trying to port this to Ruby (which is new for me). 顺便说一句,我在Python中一切正常,但是我试图将其移植到Ruby(对我来说这是新的)。 Thanks! 谢谢!

Here an example how to do that with a POST 这是一个使用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)

Try with this: 试试这个:

#!/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)

If you want to write the response into a file 如果要将响应写入文件中

File.write('filename', response)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM