2

I have created a RESTful API that has an optional parameter passed in as a URL variable. It seems to work correctly when executing from the browser directly but when trying it in the API Explorer it lists the parameter but ignores it when executing it. I have no idea where to start to look in solving this. Any help would be greatly appreciated.

The class definition is as follows:

class actions {

  /**
   * LIST available Actions
   *
   * List all the actions that a user (or app) can choose from. The response list  
   * will include [name],[slug/id], and [description] attributes. If you want a more complete set of 
   * meta attributes for the actions then you can specify "meta=all" in the request url. For full spec of
   * response please review LG_actions_list.json.
   *
   * @url GET /available
   *
   * @param $meta {@from url} Optional parameter to control the amount of meta-data passed back. Values are "none","normal", and "all"
   **/
  public function available ($meta="normal")
  {
    return "list actions (meta level set to $meta)";
  }

}

In this case I can type "all" as the value of $meta in the API explorer but the response is still "list actions (meta level set to normal)".

UPDATE:

In order to clarify this behaviour I am adding the API Explorers output and the output I get when I call the service directly:

enter image description here

In comparison, when actually using the API I get the correct results. Typing this into Chrome:

http://[domain]/api/actions/available?meta=foobar

I get the desired output of:

"list actions (meta level set to foobar)"

ken
  • 8,763
  • 11
  • 72
  • 133

1 Answers1

2

There are few problems with what you are doing with the above code

  • Optional Parameters are better left to query string
  • When you add a manual route using @url no auto routes will be added for that method

Do the following to make it work.

  • Changed the method name to get to map it to root of the class
  • Turned off smart auto routing

Now in the explorer there will be two operations listed

  • actions.json
  • actions.json/{meta}

.

class actions {

     /**
     * LIST available Actions
     *
     * List all the actions that a user (or app) can choose from. The response list
     * will include [name],[slug/id], and [description] attributes. If you want a more complete set of
     * meta attributes for the actions then you can specify "meta=all" in the request url. For full spec of
     * response please review LG_actions_list.json.
     *
     * @smart-auto-routing false
     * @param $meta Optional parameter to control the amount of meta-data passed back. Values are "none","normal", and "all"
     **/
    public function get ($meta="normal")
    {
        return "list actions (meta level set to $meta)";
    }

}
Arul Kumaran
  • 983
  • 7
  • 23
  • My original solution DID work it was only in the API Explorer that it did not. I'm not sure if I was clear about that originally. I'll update the question to reflect that. – ken Oct 12 '12 at 03:37
  • I tried your suggested solution too and it didn't work. It DID create two operations in the API Explorer but neither of these work in the API Explorer (meaning they always return the "default" value; putting a value into the provided form field has no effect on result). Similarly to my original solution, your solution does work outside of the API explorer. – ken Oct 12 '12 at 03:42
  • It works fine for me, when I use the second operation I do get the right value, just to make sure another api method is not taking its call, first comment others and then if it works fine, then change the method order to make it work. Also make sure you are using latest version of Restler 3 and Explorer – Arul Kumaran Oct 12 '12 at 05:02
  • Ok, i'll give it a try. BTW, is there anything technically wrong with my solution? I sort of like the documentation better my way as it doesn't unnecessarily clutter the number of service methods for an optional parameter that likely won't be used all that often. In this kind of scenario just having one service (but having the parameter work) would be my ideal situation. Regardless though I will retry your approach again and post the results. – ken Oct 12 '12 at 09:16