How POST binary using Ruby?

G

goodieboy

Hi,

I've got a script that uses curl, and would like (for educational
purposes mind you) to use ruby instead. This is the curl command that
works:

F="./my_data.xml"
curl 'http://localhost:8080/update' --data-binary @$F -H 'Content-
type:text/xml; charset=utf-8'

I've been messing with Net::Http using something like below, with
variations (Base64.encode64) but nothing works yet. Anyone know the
ruby equivlent to the curl version above?

Thanks!

# NOT WORKING:
my_url = 'http://localhost:8080/update'
data = File.read('my_data.xml')
url = URI.parse(my_url)
post = Net::HTTP::post.new(url.path)
post.body = data
post.content_type = 'application/x-www-form-urlencoded; charset=utf-8'
response = Net::HTTP.start(url.host, url.port) do |http|
http.request(post)
end
puts response.body
 
G

goodieboy

Got it!

data = File.read('my_data.xml')
url = URI.parse('http://localhost:8080/update')
http = Net::HTTP.new(url.host, url.port)
response, body = http.post(url.path, data, {'Content-type'=>'text/xml;
charset=utf-8'})

Matt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top