Forum: Ruby on Rails Active Resource custom method using POST

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
BenH (Guest)
on 2009-05-26 20:29
(Received via mailing list)
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
BenH (Guest)
on 2009-05-27 03:25
(Received via mailing list)
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 -
This topic is locked and can not be replied to.