Ruby undefined method `bytesize' for #<Hash:0x2954fe8>

馋奶兔 提交于 2019-11-30 11:49:28

You're sending post data as a hash. You should encode it as string.

For example Using 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

UPDATE If you want GET request, append the query string to the 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

request expects a string for its second argument, on which it invokes bytesize. You're giving it a hash, which doesn't respond to bytesize.

Use POST and not GET request

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)
}

I solved by using .to_s, it returns a 200 :D

@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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!