gpt4 book ai didi

clojure - 为什么 ring 的资源响应以 application/octet-stream 内容类型响应?

转载 作者:行者123 更新时间:2023-12-03 22:08:22 24 4
gpt4 key购买 nike

我想弄清楚为什么 Ring 的 resource-response正在选择回复 application/octet-stream内容类型。我最近更新了一些我一直在学习的示例代码,以便它使用较新的 ring-defaults .使用前 ring-defaults ,此代码响应 html内容类型。为什么现在选择八位字节流?

(ns replays.handler
(:require [compojure.core :refer [GET defroutes]]
[compojure.route :as route]
[ring.util.response :refer [response resource-response]]
[ring.middleware.json :as middleware]
[ring.middleware.defaults :refer [wrap-defaults api-defaults]]))

(defroutes app-routes
(GET "/" [] (resource-response "index.html" {:root "public"}))
(GET "/widgets" [] (response [{:name "Widget 1"} {:name "Widget 2"}]))
(route/resources "/public")
(route/not-found "not found"))

(def app
(-> app-routes
(middleware/wrap-json-body)
(middleware/wrap-json-response)
(wrap-defaults api-defaults)))

而且,对于版本号,这是项目文件......
(defproject replays "0.1.0-SNAPSHOT"

:url "http://example.com/FIXME"
:description "FIXME: write description"

:plugins [[lein-pdo "0.1.1"]
[lein-ring "0.9.3"]
[lein-cljsbuild "1.0.5"]]

:dependencies [[org.clojure/clojure "1.6.0"]
[org.clojure/core.async "0.1.346.0-17112a-alpha"]
[org.clojure/clojurescript "0.0-3126"]
[org.omcljs/om "0.8.8"]
[ring/ring-core "1.3.2"]
[ring/ring-json "0.3.1"]
[ring/ring-defaults "0.1.4"]
[compojure "1.3.2"]
[cljs-http "0.1.29"]]

:source-paths ["src/clj"]

:ring {:handler replays.handler/app}

:cljsbuild {:builds [{:id "dev"
:source-paths ["src/cljs"]
:compiler {:output-to "resources/public/js/app.js"
:output-dir "resources/public/js/out"
:optimizations :none
:source-map true}}]}

:aliases {"up" ["pdo" "cljsbuild" "auto" "dev," "ring" "server-headless"]}

:profiles {:dev {:dependencies [[javax.servlet/servlet-api "2.5"]
[ring-mock "0.1.5"]]}})

最佳答案

这是因为 api-defaults (实际上是 ring.middleware.content-type )尝试根据您传递给它的数据猜测您的内容类型。它使用文件扩展名来做到这一点,因为您使用的是 resource-response ,没有扩展名。所以默认情况下 ring.middleware.content-type用途 application/octet-stream .

解决方案是为响应提供您的内容类型,如下所示:

(ns app.routes
(:require [compojure.core :refer [defroutes GET]]
[ring.util.response :as resp]))

(defroutes appRoutes
;; ...
;; your routes
;; ...
(GET "/"
resp/content-type (resp/resource-response "index.html" {:root "public"}) "text/html"))

关于clojure - 为什么 ring 的资源响应以 application/octet-stream 内容类型响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29337676/

24 4 0