Forum: Ruby on Rails Multipart REST post, XML body with file

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
8b6fa694bf556e382b8db38efe8351c4?d=identicon&s=25 e deleflie (Guest)
on 2009-04-08 06:29
(Received via mailing list)
Hi,

Can anyone shed light on how to do a classic RAILS REST POST (i.e.
post an XML structure representing an ActiveResource) ... whilst at
the same time posting a file?

My current code (working) is below. It does not have a file associated
with the post.

I know I have to do a multipart, but what I cant work out is that in a
multipart, each value has a key ... what would be the key for the main
XML body? Because, see, in the example below, the XML _is_ the body
... it has no key associated with it.

Etienne

----------------------------------------------------------
body = "<?xml version=\"1.0\"
encoding=\"UTF-8\"?><my_object><someValue
type=\"boolean\">true</someValue><someOtherValue>3456</someOtherValue</my_object>"

    uri = URI.parse("http://#{$SITE_URL}/my_object.xml")
    http = Net::HTTP.new(uri.host, uri.port)
    headers={}
    headers['Content-Type'] = "application/xml"

    begin
      http.start do
        req = Net::HTTP::Post.new(uri.request_uri, headers)
        req.basic_auth($name, $pword)
        resp = http.request(req, body)

        if resp.is_a?(Net::HTTPSuccess)
          return true
        else
          return false
        end

      end
    rescue Exception => e
      debug "Exception with submitting using net:http #{ e } (#{ e.class
})!"
      return false
    end
This topic is locked and can not be replied to.