Net/http

I am trying to use this method to post a form.
The problem is that the set_form_data method sets Content-Type: header
field to application/x-www-form-urlencoded. I need it to be
application/xml.
I tried the set_content_type_method(‘application/xml’), but it did not
seem to help.
Here is my code
url = URI.parse(‘http://192.168.0.248/putxml’)
req = Net::HTTP::Post.new(url.path)
req.basic_auth ‘admin’, ‘pass’
req.set_form_data(‘192.168.0.122’)
res = Net::HTTP.new(url.host, url.port).start {|http|
http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

Can someone please help me. I don’t need to use this method.
I can get it what I want by typing this at the command line.
curl -H “Content-Type: application/xml” -X POST -d
“192.168.0.248”
http://admin:[email protected]/putxml

Thanks

John F wrote:

I am trying to use this method to post a form.
The problem is that the set_form_data method sets Content-Type: header
field to application/x-www-form-urlencoded. I need it to be
application/xml.
I tried the set_content_type_method(‘application/xml’), but it did not
seem to help.
Here is my code
url = URI.parse(‘http://192.168.0.248/putxml’)
req = Net::HTTP::Post.new(url.path)
req.basic_auth ‘admin’, ‘pass’
req.set_form_data(‘192.168.0.122’)

These two additional statements “do it for me”

set_form_data sets the content_type so we set it to what we need

req.content_type = ‘application/x-www-form-urlencoded’

add_field prepends HTTP_

req.add_field ‘X_REQUESTED_WITH’, ‘XMLHttpRequest’

res = Net::HTTP.new(url.host, url.port).start {|http|

http.request(req) }
case res
when Net::HTTPSuccess, Net::HTTPRedirection
# OK
else
res.error!
end

Stephan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs