Question

I am writing an api client using the RocketPants gem and am trying to use HTTParty as included in the gem to read, create, update and destroy resources.

Reading and destroying works just fine, but I am facing problems creating and updating resources (in the example below, events).

Specifically, I am trying the following:

def create_event params
  post "events", :body => { :event => params }, :transformer => Event
end

But on the API side it always gives me

Rendering error for ActionController::ParameterMissing:
key not found: event

even though I do include it in the post request.

However, when I try doing it with HTTParty directly, like so:

HTTParty.post(
  "http://localhost:3000/1/events",
  :headers => {
    'Authorization' => ActionController::HttpAuthentication::Token.encode_credentials("test token")
  },
  :body => {
    :event => {
      :name => "test"
    }
  }
)

it works, so it can't be the API that's not working. This approach has two downsides: I need to pass in authorization manually, and the result is not wrapped as an Event.

Am I missing anything to properly send post and put requests? I also looked at the docs and specs, but couldn't find anything on posting/putting (e.g., see https://github.com/filtersquad/rocket_pants/blob/master/spec/rocket_pants/client_spec.rb).

Here is my full code:

require "ticket_gate/version"
require "rocket_pants"

module TicketGate
  class Client < RocketPants::Client
    # ---- Setup -----------------------------------
    # ----------------------------------------------
    version 1
    base_uri 'http://localhost:3000'

    def initialize access_token = nil
      raise 'Please initialize with your api access token' unless access_token
      @access_token = access_token
    end

    def base_request_options
      { :headers => { 'Authorization' => ActionController::HttpAuthentication::Token.encode_credentials(@access_token) } }
    end

    # ---- Events -----------------------------------
    # -----------------------------------------------
    class Event < APISmith::Smash
      property :created_at
      property :end_at
      property :id
      property :name
      property :parent_id
      property :start_at
      property :ticket_limit
      property :updated_at
    end

    def list_events
      get "events", :transformer => Event
    end

    def get_event id
      get "events/#{id}", :transformer => Event
    end

    #### HERE IS THE PROBLEM
    def create_event params
      post "events", :body => { :event => params }, :transformer => Event
    end

    #### HERE IS THE PROBLEM
    def update_event id, params
      put "events/#{id}", :body => { :event => params }, :transformer => Event
    end

    def destroy_event id
      delete "events/#{id}", :transformer => Event
    end
  end
end
Was it helpful?

Solution

RocketPants uses API Smith internally for the HTTP requests.

From the API Smith documentation I tried to figure out how to pass POST parameters properly, and I'm guessing this is what you're supposed to do:

post "events", :extra_body => { :event => params }, :transformer => Event
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top