Skip to content

"Could not satisfy the request Accept header" with APIView and StreamingHttpResponse #67

@geekyganguly

Description

@geekyganguly

Summary

When using adrf.views.APIView with StreamingHttpResponse, the framework throws a "Could not satisfy the request Accept header" error, even when the response content type is explicitly set.

Environment

  • Python Version: 3.13
  • Django Version: 5.2.3
  • DRF Version: 3.16.0
  • ADRF Version: Latest (as of report date)

Code Reproduction

View Implementation

import asyncio
import json
from adrf.views import APIView as AsyncAPIView
from django.http import StreamingHttpResponse

class ChatStreamView(AsyncAPIView):
    async def post(self, request):
        try:
            # ... business logic ...

            # Start streaming response
            response = StreamingHttpResponse(
                self.stream_response(message, session, agent, callback_handler),
                content_type="text/event-stream",
            )

            # Set SSE headers
            response["Cache-Control"] = "no-cache"
            response["Connection"] = "keep-alive"
            response["Access-Control-Allow-Origin"] = "*"
            response["Access-Control-Allow-Headers"] = "Cache-Control"

            return response
        except Exception as e:
            return StreamingHttpResponse(
                self.error_stream(str(e)),
                content_type="text/event-stream",
            )

    async def stream_response(self, message, session, agent, callback_handler):
        """Stream the response using SSE format"""
        try:
            yield self.format_sse_event("message_start", {})
            # ... streaming logic ...
            yield self.format_sse_event("message_end", {})
        except Exception as e:
            yield self.format_sse_event("error", {"message": str(e)})

    def format_sse_event(self, event_type, data):
        """Format data as SSE event"""
        return f"event: {event_type}\ndata: {json.dumps(data)}\n\n"

    def error_stream(self, error_message):
        """Stream error message"""
        yield self.format_sse_event("error", {"message": error_message})

Django Settings

INSTALLED_APPS = [
    # ... other apps ...
    "daphne",
    "rest_framework",
    "adrf",
]

WSGI_APPLICATION = "config.wsgi.application"
ASGI_APPLICATION = "config.asgi.application"

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_RENDERER_CLASSES": [
        "rest_framework.renderers.JSONRenderer",
    ],
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions