Skip to content

Instantly share code, notes, and snippets.

@karlseguin
Created November 10, 2011 12:03
Show Gist options
  • Save karlseguin/1354705 to your computer and use it in GitHub Desktop.
Save karlseguin/1354705 to your computer and use it in GitHub Desktop.
You build an endpoint that takes in data and saves it.
This is a service that takes in data from outside sources, the user can be any type.
(some sites use integers, some use strings, some guids...)
3 samples:
POST /notes
{
user: 9000
message: 'over'
}
POST /notes
{
user: 'vegeta'
message: 'over 9000!'
}
POST /notes
{
user: '9002'
message: 'blah'
}
Now we want to let people get the data:
GET /notes/9000
id = request[:id]
Right now, id is a string. But, if I use a string to look up, It won't return anything.
GET IS FAIL
@JeffryGonzalez
Copy link

Are you saying that
GET /notes/9000 gives you :id == 9000
but
GET /notes/'vegeta' gives you :id == nil
?

@karlseguin
Copy link
Author

Pretend this is ASP.NET and we are using request.QueryString, which is a NameValueCollection.

string user = Request.QueryString["user"]

Now, say you want to find the records for this user. There's a huge difference between WHERE user = '9000' and WHERE user = 9000

This isn't a problem for json, because the data would either come in as {user: 9000} or {user: '9000'}..but for application/x-www-form-urlencoded it always comes in a strings.

@JeffryGonzalez
Copy link

Yeah - while in your domain there is a big difference between '9000' and 9000, in a URI there isn't, right? So either your domain or URIs are broken. REST is a set of constraints... you could step back and just tunnel your GET through a post. Maybe create a nice XML schema for it... ;)

I get it now. I'm having a hard time imagining where I'd be ok with have a resource that could be identified with either '9000' or 9000, though - no way to normalize it? Or even, hackish - GET /notes/9000?idtype='character' or something sick like that?

@meze
Copy link

meze commented Nov 10, 2011

and to make things worse, let's suppose that 9000 is a valid user name. Now we can't decide whether '9000' is a user name or user id ;)

@JeffryGonzalez
Copy link

You know, since the ID we are talking about here ISN'T the id of the note, but actually a query parameter (it would return a set of notes)... I'd rethink the whole route.

I'm a little more awake, now.

A route like '/Notes/9000' would be the note with the ID of 9000.

If you wanted the notes for user 9000 you might do:

/Notes?userid=9000

or

/Notes?userId='9000' (a little sketchy, but what the hell?)

or

/Notes?userName='9000'

/Notes/9000 should return a single resource, and I think you want to refine the

/Notes

route which would return a "set" of notes. In that case, I filter it with query string params, though, you could do something like:

/Notes/UserId/9000
/Notes/UserName/9000

But I don't like that as much.

@karlseguin
Copy link
Author

I'll give you an example of where this is common.

When you display disqus on your page, you specify a "disqus_identifier". This can be anything. For example all my old post use an integer, which is the id of the row in the database. All my new post use the url (a string), since I moved to github+jekyll.

Once you start talking about publicly accessible services, I think this becomes a lot more common.

(My guess is that disqus just converts everything to a string...which is pragmatic, but it makes me want to RAGE QUIT)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment