0

How can I set the Content-Type header on a Response object?

My code so far:

new Response(
    file[1],
    {
        status: 200,

        // i have also tried "contentType", and "ContentType"
        "Content-Type": "text/html",
    },
)

For background, I am trying to create a custom HTML file and put it in cache with the caches API, and it is defaulting to text/plain.

0

1 Answer 1

3

I figured it out right after posting:

new Response(
    "response body here...",
    {
        status: 200,
        headers: new Headers({
            "content-type": "text/html;charset=UTF-8",
        }),
    },
)

You need to use the Headers constructor and set it under the "headers" key, then you can set whatever response headers you want.

Sign up to request clarification or add additional context in comments.

1 Comment

For future reference: Response()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.