# Active Resource custom method using POST

**URL:** <https://www.ruby-forum.com/t/active-resource-custom-method-using-post/168856>\
**Category:** Rails\
**Created:** [May 26, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/active-resource-custom-method-using-post/168856 "2009-05-26T18:29:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8baadc/32.png) [@BenH](https://www.ruby-forum.com/u/BenH)\
**Post date:** [May 26, 2009, 6:29pm UTC](https://www.ruby-forum.com/t/active-resource-custom-method-using-post/168856/1 "2009-05-26T18:29:29Z")

</div>

I’ve written a small piece of code that is supposed to place an order  
in a order handling system. get, put and delete functions are working  
great but I cannot get post to function at all… the error I get back  
from the code below is 400 (bad request). I think it has something to  
do with the headers but I don’t know how to get more info out of this.  
Is there a problem with the way I am handling the post method?

Thank you for your input!!  
Ben

#######

class User \< ActiveResource::Base  
self.site = “http://www.a\_restful\_api.com/api/”  
self.element\_name = “user”  
self.format = :json

def place\_order(input)  
# … handle input constraints. input is a hash

```
# parse the hash to end up with "key=value&key=value&key=value"
body = ""
input.keys.sort.each do |k|
  if k == input.keys.sort.first
    body += %Q{#{k}=#{input[k]}}
  else
    body += %Q{&#{k}=#{input[k]}}
  end
end

user = User.find_by_remote_id(self.user_id)
header ||= make_header(user) # this function returns a hash with

```

the various elements in the header

```
method_string = "#{self.class.prefix}#{self.class.collection_name}/

```

#{self.user\_id}/store/orders"  
resp = connection.post(method\_string,body,header)  
end

---

<div class="post-metadata">

**Author:** ![BenH](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/8baadc/32.png) [@BenH](https://www.ruby-forum.com/u/BenH)\
**Post date:** [May 27, 2009, 1:25am UTC](https://www.ruby-forum.com/t/active-resource-custom-method-using-post/168856/2 "2009-05-27T01:25:24Z")

</div>

I may just go to the command line with curl - that seems to work  
without a hitch.  
Also, for anyone looking at creating a POST body, the code I posted  
previously for converting a hash to POST body can be completely  
replaced with {hash}.to\_param. In my case:  
body = input.to\_param

Good luck -
