Active Resource custom method using POST


#1

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


#2

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 -