2

Hi I am trying to replace php-java-bridge with restful API, in older application EJBs were accessed by PHP using php-java-bridge Using this bridging mechanism I was able to send complex Objects from PHP to Java.

Now as we have replaced the bridge with Restful API now instead of object we have to send each individual param.

Let say I have a User class consist of id, name and favouriteLinks

favrouteLinks is an array of Type Link consist of title and url.

class User {
        Integer id;
        String name;
        Link[] favouriteLinks;

}
class Link {
        String title;
        String url;
}

Let say API is trying to save a user with multiple favourite links detail. Now as you have noticed that this is a nested/hierarchical information that need to be passed. What is the correct way to achieve this?

api.something.com/1/user?id=101&name=Jon&favourite????

What if I have an array of users that have an array of favourite links????

Please share your expert thoughts.

PHP Avenger
  • 1,744
  • 6
  • 37
  • 66
  • 1
    did you give a thought for `JSON`? – Rockstar Aug 11 '14 at 10:28
  • Yes I have though about that after reading some post. Making `JSON` as the `body` of the request. but I think then API itself will not remain as clean(verbose) as specifying parameters in `URL` with name/value pair. For responses I am already using `JSON` and `XML` – PHP Avenger Aug 11 '14 at 10:38
  • 1
    clean != verbose and specifying parameters in the URL don't scale well (not only does it lead to very long URL but characters encoding problems may soon arise). In any case, if you want to be RESTful you'll have to use the body of your requests to modify data. – m4rtin Aug 11 '14 at 10:50
  • I agree with that it will result in very very long URL (first I am not sure what that long URL would be). That is why I though to consult experts. – PHP Avenger Aug 11 '14 at 11:37

1 Answers1

0

Let say API is trying to save a user with multiple favourite links detail.

If you want a RESTful API, do not attempt to modify data through a GET request. Save your favourite links either with a POST or a PUT (more about this here : PUT vs POST).

I also don't really understand how you designed your URI :

api.something.com/1/user?id=101&name=Jon&favourite????

Is 1 the version number of the API ? Then ok, but the id of the targeted resource (here a User) should definitely not be part of the query parameters. How about something like :

api.something.com/1/users/101

to designate a user resource ? You could then POST or PUT to this resource to update whatever you want, like its favourite links.

Community
  • 1
  • 1
m4rtin
  • 2,445
  • 22
  • 34
  • Thanks @m4rtin, It is just an example, yes 1 mean API version, I understand the put and post (that is just an example). I actually want to 1 - save a new user with multiple fav links. 2 - Array of users with an array of fav links. – PHP Avenger Aug 11 '14 at 11:25