1

I am writing a code that send http post request. Now I write xml body in my code, and its working correctly.

But if I want to send request using xml file I get
undefined method `bytesize' for # Did you mean? bytes

My code below

require 'net/http'

request_body = <<EOF
<xml_expamle>
EOF

uri = URI.parse('http://example')
post = Net::HTTP::Post.new(uri.path, 'content-type' => 'text/xml; charset=UTF-8')
post.basic_auth 'user','passcode'
Net::HTTP.new(uri.host, uri.port).start {|http|
  http.request(post, request_body) {|response|
    puts response.body
  }
}


**But if I want to make send file**

require 'net/http'

request_body = File.open('example/file.xml')


uri = URI.parse('http://example')
post = Net::HTTP::Post.new(uri.path, 'content-type' => 'application/xml; charset=UTF-8')
post.basic_auth 'user','passcode'
Net::HTTP.new(uri.host, uri.port).start {|http|
  http.request(post, request_body) {|response|
    puts response.body
  }
}

I get undefined method `bytesize' for # Did you mean? bytes

1 Answer 1

1

You need to load the file content to memory if you want to use it as a request body, use #read method:

request_body = File.open('example/file.xml').read

and it'll work.

Sign up to request clarification or add additional context in comments.

Comments

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.