# Using a different xml format for REST

**URL:** <https://www.ruby-forum.com/t/using-a-different-xml-format-for-rest/172300>\
**Category:** Rails\
**Created:** [July 18, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/using-a-different-xml-format-for-rest/172300 "2009-07-18T22:47:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 18, 2009, 10:47pm UTC](https://www.ruby-forum.com/t/using-a-different-xml-format-for-rest/172300/1 "2009-07-18T22:47:45Z")

</div>

I want to add a different xml format, plist in this case, to my  
controller for the create action. Using the default scaffold, I can  
handle the standard AR format. I’d like to be able to have a post to:

POST foos/create.plist  
[plist formatted object]

That means I need to parse the incoming plist and turn it into an AR  
object. That’s not hard, but I’m not sure where to do it, how to  
route the .plist request, etc. Do I need to create a new content-type  
handler? Where do I access the POST data?

Right now I have some controllers hacked with plist\_create actions and  
I access the raw POST data in env. I’d like it to look like this:

def create

# This of course requires something under the covers to recognize

the .plist request and parse it into params

# Where do I do that?

@foo = Foo.new(params[:foo])

respond\_to do |format|  
format.plist { render :xml =\> some\_response.to\_plist }  
end  
end

Thanks

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 19, 2009, 12:56am UTC](https://www.ruby-forum.com/t/using-a-different-xml-format-for-rest/172300/2 "2009-07-19T00:56:27Z")

</div>

Alex wrote:

> I want to add a different xml format, plist in this case, to my  
> controller for the create action. Using the default scaffold, I can  
> handle the standard AR format. I’d like to be able to have a post to:
> 
> POST foos/create.plist  
> [plist formatted object]  
> respond\_to do |format|  
> format.plist { render :xml =\> some\_response.to\_plist }  
> end  
> end

It sounds to me that you’re thinking in the wrong MVC layer. I would  
handle the plist format in a similar way as to\_json by extending the  
model objects with a to\_plist method. Then you simply need to register a  
mime-type for plist format and so something like:

> respond\_to do |format|  
> format.plist { render :xml =\> my\_model\_object.to\_plist }  
> end

> That means I need to parse the incoming plist and turn it into an AR  
> object. That’s not hard, but I’m not sure where to do it, how to  
> route the .plist request, etc. Do I need to create a new content-type  
> handler? Where do I access the POST data?

I’m not quite expert enough to know for sure if this will work, but you  
might look into adding your plist parsing as Rack middle-ware. This  
would be similar to the Rack middle-ware that handles the JSON parsing.

Check out [http://railslab.newrelic.com/2009/06/05/episode-14-rack-metal](http://railslab.newrelic.com/2009/06/05/episode-14-rack-metal)  
for information on how you might approach this.

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 19, 2009, 12:26pm UTC](https://www.ruby-forum.com/t/using-a-different-xml-format-for-rest/172300/3 "2009-07-19T12:26:50Z")

</div>

Regarding the way you suggest handling rendering the plist, that’s the  
plan. My example was create, and the response was arbitrary.

My question is still - where does the xml get parsed? When something  
is posted to \*.xml something is turning it into a name/value list, and  
I need to do that from a plist instead of whatever xml format it  
transforms by default.
