Tag: http

无效请求:无效的HTTP格式,Rails中的解析失败

传递具有特殊字符的字符串时,我收到无效请求:无效的HTTP格式,解析失败错误。 日志中的错误如下。 我的请求: http://localhost:3000/search/% 错误日志: Invalid request: Invalid HTTP format, parsing fails. /.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `execute’ /.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/request.rb:84:in `parse’ /.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/connection.rb:39:in `receive_data’ /.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine’ /.rvm/gems/ruby-1.9.3-p545/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run’ /.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/backends/base.rb:73:in `start’ /.rvm/gems/ruby-1.9.3-p545/gems/thin-1.6.2/lib/thin/server.rb:162:in `start’ /.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/handler/thin.rb:16:in `run’ /.rvm/gems/ruby-1.9.3-p545/gems/rack-1.5.2/lib/rack/server.rb:264:in `start’ /.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands/server.rb:84:in `start’ /.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:76:in `block in ‘ /.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `tap’ /.rvm/gems/ruby-1.9.3-p545/gems/railties-4.0.2/lib/rails/commands.rb:71:in `’ bin/rails:4:in `require’ bin/rails:4:in `’ 可能是什么问题呢? 请建议我解决这个问题的想法。 如何在获得以下错误的同时重定向到另一个页面?

Ruby HTTP POST – 错误

有人可以向我解释为什么我在这个POST时遇到这个错误? 我从Ruby-docs页面中提取了代码片段。 undefined method `hostname’ for #URI::HTTP:0x10bd441d8 URL:http://ws.mittthetwitapp.com/ws.phpmywebservice (NoMethodError) 也许我错过了一个要求或什么? require ‘net/http’ uri= URI(‘http://ws.mywebservice.com/ws.php’) req = Net::HTTP::Post.new(uri.path) req.set_form_data(‘xmlPayload’ => ‘Hi Test’) res = Net::HTTP.start(uri.hostname, uri.port) do |http| http.request(req) end case res when Net::HTTPSuccess, Net::HTTPRedirection # OK else res.value end

带API的Ruby pass头

所以我一直在尝试为Twitch设置我的聊天机器人。 我希望用户能够输入!stats然后他们会看到我的Fortnite统计数据。 他们可以使用命令和东西,这不是我遇到的问题。 我在向Fortnite Tracker API发送标题时遇到了一些麻烦。 他们想要接受这个: TRN-Api-Key: somelong-api-key-right-here在标题中有一些TRN-Api-Key: somelong-api-key-right-here 。 我试过这些方法:require(“httparty”) url = “https://api.fortnitetracker.com/v1/profile/pc/bentearzz” header = { key: “TRN-Api-Key: Somelong-api-key-here” } response = HTTParty.get(url, header: header) puts(response.body) 还有这个 require(“net/http”) require(“json”) url = “https://api.fortnitetracker.com/v1/profile/pc/bentearzz” uri = URI(url) response = Net::HTTP::Get.new(uri) response.basic_auth(“TRN-Api-Key: 7af072f0-d195-4c44-b1b4-a8838080e4c4”) JSON.parse(response) print(response) 请帮忙。

Ruby – 使用标头发送GET请求

我想用一个网站的api来使用ruby。 说明是发送带有标头的GET请求。 这些是来自网站的说明和他们提供的示例php代码。 我要计算一个HMAC哈希并将其包含在apisign头下。 $apikey=’xxx’; $apisecret=’xxx’; $nonce=time(); $uri=’https://bittrex.com/api/v1.1/market/getopenorders?apikey=’.$apikey.’&nonce=’.$nonce; $sign=hash_hmac(‘sha512’,$uri,$apisecret); $ch = curl_init($uri); curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘apisign:’.$sign)); $execResult = curl_exec($ch); $obj = json_decode($execResult); 我只是在命令提示符下使用安装在windows上的ruby的.rb文件。 我在ruby文件中使用net / http。 如何发送带标题的GET请求并打印响应?

Ruby – 如果url是重定向,如何下载文件?

如果url是重定向,Ruby如何下载文件? 我正在尝试下载此url: soundcloud.com/stereo-f—/cohete-amigo/download 重定向是这样的: [ec-media.soundcloud.com/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1352919869&Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D][2] 我试过的是这个: Net::HTTP.start(“ec-media.soundcloud.com”) { |http| resp = http.get(“/HNIGsuMJlDhy?ff61182e3c2ecefa438cd0210ad0e38569b9775ddc9e06b3c362a686319250ea5c1ae2d33d8d525807641f258e33de3cb0e559c1b591b5b00fb32d5ef9&AWSAccessKeyId=AKIAJ4IAZE5EOI7PA7VQ&Expires=1352919869&Signature=OVWD9VdV7ew%2B%2Fs%2BO0YpkKZLGOCw%3D”) open(“test.wav”, “wb”) { |file| file.write(resp.body) } } puts “Done.” sleep 50 我不明白/知道任何互联网协议和重定向和那些东西……请给我一个很好的解释或帮助我的脚本从rubcl下载soundcloud的文件? 谢谢 更新 : 我试过这种方式,但我收到一个错误: Net::HTTP.start(“soundcloud.com”) do |http| resp = http.get(“/dubstep-4/kill-paris-tender-love/download”) while resp.code == ‘301’ || resp.code == ‘302’ # moved permanently or temporarily: try again at the new location. resp = […]

使JRubyinheritanceJava代理设置

我想从JRuby上运行的Rails代码发出HTTP请求。 我怎样才能重新使用http.proxyHost,http.proxyPort和http.nonProxyHosts设置,给JVM运行它?

使用Ruby将XML请求发布到Web服务器

我担心我在Web服务器上发布文档(例如XML)的经验不多,所以如果我对HTTP的理解不足,我会道歉。 我在127.0.0.1端口2000上的ruby应用程序中设置了一个基本的Mongrel Web服务器。 (服务器)。 我在同一台计算机上运行一个单独的Ruby应用程序。 (客户端)。 我需要客户端将XML文档POST到服务器。 我曾尝试使用Net :: HTTP来做到这一点,但我找不到一个明确的例子,告诉我应该做些什么。 我有一个去,但遇到了错误。 我已经打破了请求,使其尽可能基本: http = Net::HTTP.new(“127.0.0.1”, 2000) http.post(‘file’, ‘query=foo’) #xc.rb line 6 但它会导致以下错误 C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `read_nonblock’: An existing connection was forcibly closed by the remote host. (Errno::ECONNRESET) from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:141:in `rbuf_fill’ from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:122:in `readuntil’ from C:/Ruby193/lib/ruby/1.9.1/net/protocol.rb:132:in `readline’ from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2562:in `read_status_line’ from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:2551:in `read_new’ from C:/Ruby193/lib/ruby/1.9.1/net/http.rb:1319:in `block in transport_request’ […]

在加载网页时:“未定义的方法`request_uri’为#”

我正在尝试使用Ruby通过HTTP加载网页并检查其状态代码是什么。 我的代码如下所示: require “net/http” @r = Net::HTTP.get_response(URI.parse(myURL)) return @r.code 但是,对于某些URL(大多数指向像Web计数器这样不会给出正确响应的奇怪内容)我正在为undefined method request_uri for #获取undefined method request_uri for # 。 我已经将它追溯到http.rb的第380行(我正在运行Ruby 1.8),它说: def HTTP.get_response(uri_or_host, path = nil, port = nil, &block) if path host = uri_or_host new(host, port || HTTP.default_port).start {|http| return http.request_get(path, &block) } else uri = uri_or_host new(uri.host, uri.port).start {|http| return http.request_get(uri.request_uri, &block) <— […]

如何模拟Net :: HTTP :: Post?

是的,我知道最好使用webmock,但我想知道如何在RSpec中模拟这个方法: def method_to_test url = URI.parse uri req = Net::HTTP::Post.new url.path res = Net::HTTP.start(url.host, url.port) do |http| http.request req, foo: 1 end res end 这是RSpec: let( :uri ) { ‘http://example.com’ } specify ‘HTTP call’ do http = mock :http Net::HTTP.stub!(:start).and_yield http http.should_receive(:request).with(Net::HTTP::Post.new(uri), foo: 1) .and_return 202 method_to_test.should == 202 end 测试失败,因为似乎试图匹配NET :: HTTP :: Post对象: […]

我应该使用什么URL来进行更新?

我有一个我刚拿起的Rails应用程序,它在“rake routes”中有以下命令: PUT /testclass/:id(.:format) testclass#update 我想发送一个PUT更新到这个测试testclass , id 18445,并将finished更改为false: /18445&finished=false 例如。 我的理解是这应该可以通过浏览器中的HTTP请求来完成,例如,可以吗? 或者我需要使用Ruby命令吗? 任何指导?