# Resource create action with json

**URL:** <https://www.ruby-forum.com/t/resource-create-action-with-json/210218>\
**Category:** Rails\
**Created:** [August 2, 2011, 4:46pm UTC](https://www.ruby-forum.com/t/resource-create-action-with-json/210218 "2011-08-02T16:46:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 2, 2011, 4:46pm UTC](https://www.ruby-forum.com/t/resource-create-action-with-json/210218/1 "2011-08-02T16:46:07Z")

</div>

Hi,

Regularly, you create a resource in html via form. you ve got some  
naming conventions for form fields and rails will bring it together  
for you in params hash.

My question is, how my i do this with json. I would like to create a  
resource with pure json.

do I have to follow specific naming,syntax convention also in order to  
get everything in my params hash ?

Just a simple example, Im doing http post for player resource with  
following body:  
{“player”: {“firstname”:“Bob”,“lastname”:“Alice”}}

If I do a params.each do key, value in my create action  
I get my json as a key ???

–  
Volker

---

<div class="post-metadata">

**Author:** ![vhochstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vhochstein](https://www.ruby-forum.com/u/vhochstein)\
**Post date:** [August 2, 2011, 5:01pm UTC](https://www.ruby-forum.com/t/resource-create-action-with-json/210218/2 "2011-08-02T17:01:34Z")

</div>

On Tue, Aug 2, 2011 at 9:47 PM, vhochstein  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> get everything in my params hash ?
> 
> Just a simple example, Im doing http post for player resource with  
> following body:  
> {“player”: {“firstname”:“Bob”,“lastname”:“Alice”}}

This is the right format of the params to be posted. Before you post it,  
you  
need to encode it:

JSON.encode({“player”: {“firstname”:“Bob”,“lastname”:“Alice”}})

and then set Content-Type to application/json

If I do a params.each do key, value in my create action

> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

–  
Tower He

---

<div class="post-metadata">

**Author:** ![vhochstein](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/b5e925/32.png) [@vhochstein](https://www.ruby-forum.com/u/vhochstein)\
**Post date:** [August 3, 2011, 11:15am UTC](https://www.ruby-forum.com/t/resource-create-action-with-json/210218/3 "2011-08-03T11:15:18Z")

</div>

Thanks a lot.  
I copied code for index action to create and I was setting Accept  
header to applciation/json.

Fixed that and it runs as expected. Great. 🙂

–  
Volker
