1

Im new to network programming and now have a problem with sending some xml data to a server.

I have the following code:

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="034234"></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 'hidden', 'hidden'
response = nhttp.start {|http|
  http.request(request, xml: xml)
}

puts response.body

Although i get some response, it is not the right one. For some reason it isn`t sending the body(my xml) properly. If i type the URL manually:

https://cig.dhl.de/services/sandbox/rest/sendungsverfolgung?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="034234"></data></data>

it works.

---------------------------EDIT-------------------------------

I edited my code above following the solution from Alex Wayne. Now I get this error:

d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:179:in `send_request_with_body': undefined method `bytesize' for #<Hash:0x29bcff8> (NoMethodError)

1 Answer 1

3

HTTP GET requests cannot have a body, so it the second argument is for data that gets appended to the url as the query string. You probably want to use POST which supports a real request body.

http.request_post(request, xml)

UPDATE: I think I misunderstood...

According to the query string that works, it's working with GET. But it's expecting the XML to be after ?xml=. So you need to encode you request body/query string as a hash with xml as the key and the xml string as the value.

So not this:

http.request(request, xml)
# GET http://domain.com/path?<myxml></myxml>

But this:

http.request(request, xml: xml)
# GET http://domain.com/path?xml=<myxml></myxml>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, but now i get an error d:/Ruby200/lib/ruby/2.0.0/net/http/generic_request.rb:179:in send_request_with_body': undefined method bytesize' for #<Hash:0x298cff8> (NoMethodError).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.