24

i have the following Ruby Code, for a tracking website in sandbox mode:

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.

Crusader633
  • 595
  • 1
  • 4
  • 17

4 Answers4

44

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
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thanks, that solves my problem, but generates a new one ^^ Probably you can help me with this too: The server expects the following format: https ://cig.dhl.de/services/sandbox/rest/sendungsverfolgung/?xml= i thought that this will achieve that, but obviously it doesnt. Do you have a solution for that too? – Crusader633 Feb 15 '14 at 16:03
  • @Crusader633, I updated the answer. BTW, are you sure the server expect GET ? – falsetru Feb 15 '14 at 16:08
2

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.

user229044
  • 232,980
  • 40
  • 330
  • 338
  • how should i change my request? The browser needs the following format : link?xml= ... i cant think off how else to achieve that – Crusader633 Feb 15 '14 at 15:56
1

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)
}
bjhaid
  • 9,592
  • 2
  • 37
  • 47
0

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

@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

A.D.
  • 4,487
  • 3
  • 38
  • 50
jimmy
  • 11
  • 3