#<Hash:0x2954fe8> 的 Ruby 未定义方法 `bytesize'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21799817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ruby undefined method `bytesize' for #<Hash:0x2954fe8>
提问by Crusader633
i have the following Ruby Code, for a tracking website in sandbox mode:
我有以下 Ruby 代码,用于沙盒模式下的跟踪网站:
require "net/http"
require "net/https"
require "uri"
xml = <<XML
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><data appname="dhl_entwicklerportal" language-code="de" password="Dhl_123!" request="get-status-for-public-user"><data piece-code="00340433836536550280"></data></data>
XML
uri = URI('https://cig.dhl.de/services/sandbox/rest/sendungsverfolgung')
nhttp = Net::HTTP.new(uri.host, uri.port)
nhttp.use_ssl=true
nhttp.verify_mode=OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Get.new(uri)
request.basic_auth 'xpackageWP', 'hidden'
response = nhttp.start {|http|
http.request(request, xml:xml)
}
puts response.body
I always get the error :
我总是收到错误:
d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body' undefined method `bytesize' for #<Hash:0x2954fe8> (NoMethodError)
from d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:130:in `exec'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1404:in `block in transport_request'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1403:in `catch'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1403:in `transport_request'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:1376:in `request'
from D:/Dropbox_5BHIF/Dropbox/TempDHL/Main.rb:17:in `block in <main>'
from d:/Ruby200/lib/ruby/2.0.0/net/http.rb:852:in `start'
from D:/Dropbox_5BHIF/Dropbox/TempDHL/Main.rb:16:in `<main>'
I tried really hard to solve this, but i cannot think of any problem. When i test it in the Browser with the Link and then a ?xml= it works perfectly, so it seems to be a problem with my Ruby Code.
我真的很努力地解决这个问题,但我想不出任何问题。当我使用链接在浏览器中测试它然后一个 ?xml= 它完美地工作时,所以它似乎是我的 Ruby 代码的问题。
回答by falsetru
You're sending post data as a hash. You should encode it as string.
您将发布数据作为哈希发送。您应该将其编码为字符串。
For example Using URI::encode_www_form:
例如使用URI::encode_www_form:
request = Net::HTTP::Post.new(uri)
...
response = nhttp.start do |http|
post_data = URI.encode_www_form({xml: xml})
http.request(request, post_data)
end
UPDATEIf you want GET request, append the query string to the url.
UPDATE如果您想要 GET 请求,请将查询字符串附加到 url。
post_data = URI.encode_www_form({xml: xml})
uri = URI('https://cig.dhl.de/services/sandbox/rest/sendungsverfolgung?' +
post_data)
...
response = nhttp.start do |http|
http.request(request)
end
回答by meagar
requestexpects a string for its second argument, on which it invokes bytesize. You're giving it a hash, which doesn't respond to bytesize.
request它的第二个参数需要一个字符串,它调用bytesize. 你给它一个哈希,它不响应bytesize.
回答by bjhaid
Use POSTand not GETrequest
使用POST而不是GET请求
xml = <<XML
<?xml version="1.0" encoding="ISO-8859-1" standalone="no"?><data
appname="dhl_entwicklerportal" language-code="de" password="Dhl_123!" request="get-status-for-public-user"><data piece-code="00340433836536550280"></data></data>
XML
uri = URI('https://cig.dhl.de/services/sandbox/rest/sendungsverfolgung')
nhttp = Net::HTTP.new(uri.host, uri.port)
nhttp.use_ssl=true
nhttp.verify_mode=OpenSSL::SSL::VERIFY_NONE
request = Net::HTTP::Post.new(uri)
request.body = xml
request.basic_auth 'xpackageWP', 'hidden'
response = nhttp.start {|http|
http.request(request)
}
回答by jimmy
I solved by using .to_s, it returns a 200 :D
我使用 .to_s 解决了,它返回 200 :D
@client.job.create_or_update(job_name, job_xml.get_xml.to_s)
@client.job.create_or_update(job_name, job_xml.get_xml.to_s)
this is jenkins api client what I'm currently using https://github.com/arangamani/jenkins_api_client
这是我目前使用的 jenkins api 客户端 https://github.com/arangamani/jenkins_api_client

