Jump to content

Searching REST API


NotionCommotion
Go to solution Solved by Psycho,

Recommended Posts

Say I have the following API.

GET /users # list of users
GET /users/1 # get user with id 1
POST /users # create new user
PUT /users/1 # modify user with id 1
DELETE /users/1 # delete user with id 1

GET /users will return the following:

[
   {firstname:"John",lastname:"Doe",email:"johndoe@example.com",phone:"(800) 555-1212"},
   {firstname:"Jane",lastname:"Doe",email:"janedoe@example.com",phone:"(800) 555-2121"}
]

Now I wish to return only users who's name starts with "Joh" and the client only needs first and last names.

 

GET /users/Joh won't work as there is no user "Joh".  It is my understanding that I should not use verbs and NOT do something like GET /searchUsers/Joh, correct?  Should it be GET /users?term=Joh?  For the limited data, should I just return everything and let the client deal with it, or have the client send another parameter in the URL to limit such as GET /users?term=Joh&format=minimal?

Edited by NotionCommotion
Link to comment
Share on other sites

I don't understand what the question is here.  Do you only ever want to return users whose first name starts "joh" or do you want it as an option, what are you using to as a data store? what's the issue with scripting the query dependant on the get value?  such as :

 

$v = trim($_GET['value']);
if($v == ''){
//return full dataset
}
elseif (is_int((int)$v)){
//return record with ID number
}
else{
//return results of first name search
}
Link to comment
Share on other sites

 

I don't understand what the question is here.  Do you only ever want to return users whose first name starts "joh" or do you want it as an option, what are you using to as a data store? what's the issue with scripting the query dependant on the get value?  such as :

 

Hi Muddy,

 

The returned users will be based on what the user types, and is an option and not always "joh".

 

The question is regarding REST endpoints, and whether the get all users and search users should be the same endpoint.  I've also read posts where people stated POST should be used for searches so that query parameters could be included in the body, but that just doesn't seem right.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.