Skip to content

08 Media Types Hal Json

DodaTech 1 min read

title: Media Types for HATEOAS — application/hal+json weight: 18 date: 2026-06-28 lastmod: 2026-06-28 description: Use media types like application/hal+json and application/vnd.api+json for HATEOAS APIs to indicate hypermedia support and enable format-specific client processing. tags: [api-development, hateoas]


Media types for HATEOAS use structured content types like application/hal+json, application/vnd.api+json (JSON API), and application/vnd.siren+json to indicate that a response contains hypermedia controls, enabling format-specific client processing.

```python
from flask import Flask, jsonify, request

app = Flask(__name__)

@app.route("/api/users/42")
def get_user():
    user = {"id": 42, "name": "Alice"}

    if request.accept_mimetypes.accept_json and "hal" not in request.headers.get("Accept", ""):
        return jsonify(user)  # Plain JSON

    if "application/hal+json" in request.headers.get("Accept", "application/json"):
        # Return HAL response
        hal_response = {
            "_links": {
                "self": {"href": "/api/users/42"},
                "orders": {"href": "/api/users/42/orders"}
            },
            "id": user["id"],
            "name": user["name"]
        }
        response = jsonify(hal_response)
        response.headers["Content-Type"] = "application/hal+json"
        return response

    return jsonify(user)

Content negotiation via Accept header lets clients express hypermedia format preference. Return standard application/json by default and application/hal+json when specifically requested.

What's Next

Now learn about HATEOAS clients in Hypermedia APIs and HATEOAS.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro