# How do I add content-type to HTTP.jl server?

**URL:** <https://discourse.julialang.org/t/how-do-i-add-content-type-to-http-jl-server/94725>\
**Category:** General Usage\
**Created:** [February 16, 2023, 2:53pm UTC](https://discourse.julialang.org/t/how-do-i-add-content-type-to-http-jl-server/94725 "2023-02-16T14:53:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeroexcuses](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zeroexcuses/32/46846_2.png) [@zeroexcuses](https://discourse.julialang.org/u/zeroexcuses)\
**Post date:** [February 16, 2023, 2:53pm UTC](https://discourse.julialang.org/t/how-do-i-add-content-type-to-http-jl-server/94725/1 "2023-02-16T14:53:02Z")

</div>

Main problem here is, when serving \*.js file:

```julia
Failed to load module script: Expected a JavaScript module script but the server responded with a MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.

```

How do I add content-type to the HTTP.jl server ?

Existing code:

```julia
using HTTP

const HOST = "127.0.0.1"
const PORT = 8080

println("Server Started")

router_8080 = HTTP.Router();
router_8001 = HTTP.Router();

const www_root = "/voxel/www/"

function do_8080(r::HTTP.Request)
    r.target == "/" && return HTTP.Response(200, read(www_root * "index.html"))
    local path = www_root * sanitize_path(r.target);
    return isfile(path) ? HTTP.Response(200, read(path)) : HTTP.Response(404)
end

function sanitize_path(s :: String)
    local t = split(s, "/");
    local t = filter(s -> s != "." && s != "..", t)
    return join(t, "/")
end

function do_8001(::HTTP.Request)
    return HTTP.Response(200, "Hello World 8001")
end

HTTP.register!(router_8080, "GET", "/", do_8080)
HTTP.register!(router_8080, "GET", "*", do_8080)

HTTP.register!(router_8001, "GET", "/", do_8001)
HTTP.register!(router_8001, "GET", "*", do_8001)

@async HTTP.serve(router_8080, "127.0.0.1", 8080)
HTTP.serve(router_8001, "127.0.0.1", 8001)

```

---

<div class="post-metadata">

**Author:** ![chris-b1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chris-b1/32/14165_2.png) [@chris-b1](https://discourse.julialang.org/u/chris-b1)\
**Post date:** [February 16, 2023, 7:07pm UTC](https://discourse.julialang.org/t/how-do-i-add-content-type-to-http-jl-server/94725/2 "2023-02-16T19:07:02Z")

</div>

Side note: if this is going to be publicly facing you’d probably be better off with a battle-tested file server that has more robust path sanitizing, support of range requests, etc. E.g. you could run nginx, have it serve static files, then reverse proxy anything to julia that needs to be.

That said, Content-Type is just passed as a header.

```julia
HTTP.Response(200,
    ["Content-Type" => "application/javascript"], 
    read(path))

```

HTTP.jl doesn’t have anything built-in to map file extensions to mimetypes, but e.g. you could start with a list like [this one](https://github.com/python/cpython/blob/3.9/Lib/mimetypes.py#L412) from python mapping commonly used extensions.
