Skip to content

Response as a content "application/pdf" gets returned as response.text() #981

@Bringordie

Description

@Bringordie

Same issue as in #802

My goal is to get a "application/pdf" and serve it to my users something like:

const fileURL = URL.createObjectURL(data);
window.open(fileURL, '_blank');

I'm receiving "application/pdf" from our backend. I do not have enough knowledge of what my buddy is doing is the correct way. But he insists on sending it this way with this response header.

        "responses" : {
          "200" : {
            "description" : "Successful",
            "content" : {
              "application/pdf" : {
                "schema" : {
                  "type" : "string",
                  "format" : "binary"
                }
              }
            }
          },

Adding something like:

else if (contentType === 'application/pdf') {
    return await response.blob();
}

to getResponseBody would fix this issue:

async function getResponseBody(response: Response): Promise<any> {
    try {
        const contentType = response.headers.get('Content-Type');
        if (contentType) {
            const isJSON = contentType.toLowerCase().startsWith('application/json');
            if (isJSON) {
                return await response.json();
            } else if (contentType === 'application/pdf') {
                return await response.blob();
            } else {
                return await response.text();
            }
        }
    } catch (error) {
        console.error(error);
    }
    return null;
}

I've been unable to find another way to get this to work. Googeling I've tried to make a .text() (which is what I recieve) into a Blob, but that seems to not work either.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions