ruby 带有自定义参数HTTP请求路径

ozxc1zmp  于 12个月前  发布在  Ruby
关注(0)|答案(1)|浏览(111)

我尝试在Ruby中使用Net::HTTP::Get在URL中传递一个值,我需要这样做:
https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}
但我不知道怎么做,这是我的代码:

require 'net/http'
require 'net/https'
require 'json'  

uri = URI.parse("https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri.request_uri)
request["Authorization"] = "Bearer #{'1fd64146-xxxx-xxxx-xxxx-xxxx997c170'}"
request["Accept"] = "application/vnd.bluebadge-api.v1+json"
response = http.request(request)

当我试着去做的时候,我收到了这样的信息:错误的URI(不是URI?):“https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}”
但是如果我尝试在url中插入我的param,一切都按预期工作:

基本上我的问题是,如何在我的URI中添加我的URL的这一部分:{18497418000111}**使用json我需要添加一个键/值,但在我的示例中我只需要传递值。
谢谢你,谢谢!

mrzz3bfm

mrzz3bfm1#

当我试着去做的时候,我收到了这样的信息:错误的URI(不是URI?):“https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}”
如果你用十六进制代码编码{}字符,那么它就可以工作。请参阅下面的irb输出。

编码前

3.1.2 :013 > uri = URI.parse('https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}')
.../.rvm/rubies/ruby-3.1.2/lib/ruby/3.1.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "https://cdpj.partners.bancointer.com.br/pix/v2/webhook/{18497418000111}" (URI::InvalidURIError)

编码后

3.1.2 :015 > uri = URI.parse("https://cdpj.partners.bancointer.com.br/pix/v2/webhook/%7B18497418000111%7D")
 => #<URI::HTTPS https://cdpj.partners.bancointer.com.br/pix/v2/webhook/%7B18497418000111%7D> 
3.1.2 :016 > http = Net::HTTP.new(uri.host, uri.port)
3.1.2 :017 > http.use_ssl = true
 => true 
3.1.2 :018 > request = Net::HTTP::Get.new(uri.request_uri)
 => #<Net::HTTP::Get GET> 
3.1.2 :019 > request["Authorization"] = "Bearer #{'1fd64146-xxxx-xxxx-xxxx-xxxx997c170'}"
3.1.2 :020 > request["Accept"] = "application/vnd.bluebadge-api.v1+json"
 => "application/vnd.bluebadge-api.v1+json" 
3.1.2 :021 > response = http.request(request)
 => #<Net::HTTPBadRequest 400 Bad Request readbody=true> 
3.1.2 :022 >

相关问题