15
Web + frameworks / Intermediate

HTTP, REST APIs, and backend design

Requests, responses, status codes, JSON APIs, validation, idempotency, pagination, authentication, and API boundaries.

httprestapijsonbackend

01HTTP model

MethodTypical purpose
GETRead without changing state
POSTCreate/command
PUTReplace resource
PATCHPartial update
DELETEDelete resource

The method, path, headers, body, authentication context, and server state determine a request. The response includes a status code, headers, and usually a representation such as JSON or HTML.

02Status codes

  • 200/201: successful read/create.
  • 204: success with no response body.
  • 400: malformed or invalid request.
  • 401: authentication required/invalid.
  • 403: authenticated but not authorized.
  • 404: resource not found.
  • 409: state conflict.
  • 422: semantically invalid input when your API convention uses it.
  • 429: rate limited.
  • 500: unexpected server failure.

03Small Flask JSON endpoint

pythonRead-only endpoint
@bp.get('/api/projects')
def api_projects():
    rows = Project.query.filter_by(published=True).all()
    return {
        'projects': [
            {'slug': row.slug, 'name': row.name}
            for row in rows
        ]
    }