Grape post with XML payload question

258 views
Skip to first unread message

John Burmeister

unread,
Sep 7, 2013, 12:32:11 AM9/7/13
to ruby-...@googlegroups.com
Hi all, new to Grape and have a few quick XML post questions.  

When posting xml to a grape API how do allow a "root" element?

I have a feeling I'm doing it all wrong... However if I take out the requires info below, the XML post creates it fine with the payload:

<comment>
<title>test123</title>
<content>test content</content>
</comment>

Is there a way to support both an XML post and json post with the requires info? I googled and searched for hours and couldn't find a solid example of a XML post to Grape. 

Example:

resource :comments do

  params do
    requires :content, type: String, desc: "content"
    requires :title, type: String, desc: "title"
  end

  post do
        authenticate!
        comment = @user.comments.create!({
                                          title: params[:comment][:title],
                                          content: params[:comment][:content],
                                          })

        {:status => "Created",:comment => comment.id, :user=> @user.name}
  end


Thank you
John


John Burmeister

unread,
Sep 7, 2013, 1:04:57 AM9/7/13
to ruby-...@googlegroups.com
Ok, somehow I completely missing the group option.

So now I have the below, which works fine.  However If I also wanted to support json without the need for a root element (because its not XML) what do I do?  

-----------

resource :comments do

  params do
    group :comment do
     requires :content, type: String, desc: "content"
     requires :title, type: String, desc: "title"
    end
  end

  post do
        authenticate!
        comment = @user.comments.create!({
                                          title: params[:comment][:title],
                                          content: params[:comment][:content],
                                          })

        {:status => "Created",:comment => comment.id, :user=> @user.name}
  end

------------------

I do not see a way to do for example the below and make the first two requirements optional only if the group [:issues] doesn't exist.

  params do
    requires :content, type: String, desc: "content"
    requires :title, type: String, desc: "title"
    group :comment do
     requires :content, type: String, desc: "content"
     requires :title, type: String, desc: "title"
    end
  end

Again, I might be looking at this totally wrong, open to suggestions.

Thanks
John

Daniel Doubrovkine

unread,
Sep 9, 2013, 9:36:31 AM9/9/13
to ruby-...@googlegroups.com
So what do you get with this now with XML vs. JSON? I think that if you want the same data to render differently in different formats (you want a room in XML, but not in JSON), you either need to handle it explicitly or in, for example, a custom formatter (that would remove the root).


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

John Burmeister

unread,
Sep 10, 2013, 2:28:29 PM9/10/13
to ruby-...@googlegroups.com
Quick question.

Based on the below, what would be a valid XML post to this? Is there even one?  I noticed that by default Grape returns xml as 
<hash>
<element></element>
</hash>

But it wont accept a post for the below example:
<hash>
<title>test</title>
<content>test</content>
</hash>

I'm trying to understand if there is some type of default root element that grape is expecting and if it even works this way at all. 

Daniel Doubrovkine

unread,
Sep 11, 2013, 1:16:43 PM9/11/13
to ruby-...@googlegroups.com
The short answer is that I don't know. Relatively few people have been doing XML with Grape, especially for POSTing. I'd like to fix any of these issues and clarify things. Want to help? Add some XML samples to https://github.com/dblock/grape-on-rack and I'll debug reproducible problems?
Reply all
Reply to author
Forward
0 new messages