Jones
Jones

Reputation: 1214

Setting HTTP response headers with Kemal

I want implement this answer in Kemal.

My current setup has a pdf file in app/public/map.pdf, and the following code in my main crystal file:

require "kemal"

#...

get "/map.pdf" do |env|
    env.response.headers["Content-Type"] = "application/pdf"
    env.response.headers["Content-Disposition"] = %(inline;filename="myfile.pdf")
end

Kemal.run

When I test my code by opening localhost:3000/map.pdf in a browser (firefox), it prompts to download the file (while I want it to attempt to display it). And curl -I results in the following:

HTTP/1.1 200 OK
Connection: keep-alive
X-Powered-By: Kemal
Content-Type: application/octet-stream
ETag: W/"1494983019"
Content-Length: 1170498

Where I would hope to see Content-Type: application/pdf.

Upvotes: 1

Views: 630

Answers (2)

kapad
kapad

Reputation: 48

Where I would hope to see Content-Type: application/pdf.

The point is that Content-Disposition doesn't exist as a header. Maybe you don't send any headers at all for some reason.

For example in PHP, if your script outputs a single byte before it sends the headers then no headers will be sent; only the headers before the first output. If you set a http header after some data is sent then the header will be ignored.

Upvotes: -2

Serdar Dogruyol
Serdar Dogruyol

Reputation: 5157

Kemal author here,

If the headers are ok you should be good to go with send_file. However be sure that the route name and file are not the same. In this case the route is /pdf

get "/pdf" do |env|
  env.response.headers["Content-Type"] = "application/pdf"
  env.response.headers["Content-Disposition"] = %(inline;filename="map.pdf")
  send_file env, "./public/map.pdf"
end

Upvotes: 3

Related Questions