Jump to content

Designing a web API. Best practices?


ttocskcaj

Recommended Posts

I'm developing a REST API for a website. As far as I know, the rules for RESTfulness are sort of broad. I'm just wondering if there are any good resources with tighter guidelines to developing a good, useable API. What are the common practices etc.

Some of the things I'm unclear on are:

Should it use GET and POST, or just one of them?

From what I've read about HTTP, GET is a "safe method" and shouldn't allow data to be changed on the server. Should we use GET to retrieve data off the server, and POST to save data to the server?

Or is it common practice to use POST for getting and storing data?

 

Should we utilize the HTTP status codes?

Should the HTTP status change when things go wrong? Should it be 400 if something doesn't work properly? Or 404 if the method doesn't exist?

If the request is successful, and there's no data to return, should it be 204 No Data or 200 with JSON/XML that shows it was successful?

Link to comment
Share on other sites

In REST, POST's meaning is a bit more pure than it is for web stuff. POST is for making changes - not retrieving data. Specifically it's supposed to be for creating things. There's also PUT which is like POST but can also be for updating. In reality they're both basically the same thing and I'd treat them the same way.

Besides GET the other common operations are DELETE (which does exactly what it sounds like) and HEAD (which gets information about the resource without actually sending the resource).

 

Status codes are important and have basically the same meaning in REST. You actually get to use more than standard webpages and websites would use.

Read through this list and try to obey the semantics for everything, but you don't have to use everything there. Like it wouldn't be a crime to send a 200 OK even if there's no content.

Link to comment
Share on other sites

The reason I'd want to have content on a 200 status is because some users may be only using the content and ignoring the status codes.

 

I did realize there were PUT and DELETE methods as well, however lots of web apps I've looked at use only GET or only POST, and still claim to be RESTful.

Link to comment
Share on other sites

I did realize there were PUT and DELETE methods as well, however lots of web apps I've looked at use only GET or only POST, and still claim to be RESTful.

 

This is generally because browser forms don't support PUT or DELETE.

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.