[Answer]-Django api api/add/2/+/3 vs api/method=add&expression=2+3

1👍

There is nothing specific about making an API in django. REST principles applies whatever web framework you used.

In short, there are 4 main HTTP verbs:

GET - for listing resources or retrieving details
POST - for creating resources
PUT - for replacing/updating resources
DELETE - for deleting resources

Query strings with GET should be used for search queries. For example:

  1. Wolfram Alpha uses url-encoded string for querying about mathematical expression: https://www.wolframalpha.com/input/?i=2%2B3
  2. Google uses url-encoded string for its search query: https://www.google.com.au/search?q=2%2B3

If your mathematical expression creates new resources, they should be sent in the POST body, usually either url-encoded or for more complex resources in JSON as XML. Likewise, with PUT for updating, and DELETE for deleting. The resources you need to create/update/delete should be identified by its URL.

If you want to support multiple formats (e.g. XML, JSON) or multiple languages of the same resource, you should use Content Negotiation. Also, following HATEOAS principle is highly recommended, all URLs used within the service should be discoverable within the service itself, instead of having to be inferred from a separate documentation.

Leave a comment