Skip to content

Not getting user user given value for "Accept" header from swagger ui. #4438

@prateek9623

Description

@prateek9623

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google "How to X in FastAPI" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

from fastapi import FastAPI, Header
from typing import Optional
import cv2
import io
import numpy as np
from fastapi.responses import StreamingResponse, JSONResponse

app = FastAPI()

@app.get("/image")
def get_image(accept: Optional[str] = Header("image/png")):
    accept_headers = accept.split(",")
    dummy = np.zeros((512, 512))
    if "image/png" in accept_headers or "*/*" in accept_headers:
        return StreamingResponse(
            io.BytesIO(cv2.imencode(".png", dummy)[1]), media_type="image/png"
        )
    elif "image/jpeg" in accept_headers:
        return StreamingResponse(
            io.BytesIO(cv2.imencode(".jpeg", dummy)[1]), media_type="image/jpeg"
        )
    else:
        return JSONResponse(
            status_code=406,
            content={
                "message": "encoding of type {} is not supported".format(accept)
            },
        )

Description

Hi, I have an API that is sending responses in different encoding depending on the user and to detect which encoding server should use I am using the "Accept" header. But when I tried to test it using Swagger UI, it always sent "application/json".

Operating System

Linux, Windows

Operating System Details

No response

FastAPI Version

0.70.1

Python Version

Python 3.9.5

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions