luek baja
luek baja

Reputation: 1674

JavaScript: Set Content-Type header on Response Object

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.

Upvotes: 0

Views: 2520

Answers (1)

luek baja
luek baja

Reputation: 1674

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.

Upvotes: 3

Related Questions