如何在 Ring 处理程序中获取 HttpServletRequest?

发布于 2024-12-13 13:28:53 字数 1269 浏览 0 评论 0原文

有没有办法在 Ring 处理程序中获取 HttpServletRequest 对象? 我正在使用 Noir 开发一个网络应用程序。我需要在处理 URI 时获取 HttpServletRequest 对象。所以我使用 (noir.request.ring-request) 函数来获取包含 :servlet-request 键的环请求映射,但该值为零。这是正确的方法还是我错过了什么?

以下是代码:

(ns my-app
     (:use noir.request))
(defpage [:get "/app"] []
     (str (ring-request)))

结果:

{:remote-addr "127.0.0.1", :scheme :http, :query-params {}, :session {}, :form-params {}, :multipart-params {}, :servlet #, :request-method :get, :query-string nil, :content-type nil, :cookies {"ring-session" {:value "eb509a65-d33a-40d2-9646-e2ff785428b0"}}, :uri "/app", :server-name "127.0.0.1", :params {}, :headers {"cookie" "ring-session=eb509a65-d33a-40d2-9646-e2ff785428b0", "accept-charset" "GBK,utf-8;q=0.7,*;q=0.3", "accept-language" "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4", "accept-encoding" "gzip,deflate,sdch", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "user-agent" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2", "connection" "keep-alive", "host" "127.0.0.1:8080"}, :servlet-request #, :content-length nil, :server-port 8080, :character-encoding nil, :servlet-response #, :body #, :servlet-context #}

Is there a way to get the HttpServletRequest object in a Ring handler?
I am using Noir to develope a web app. I need to get the HttpServletRequest obj when handling a URI. So I use the (noir.request.ring-request) function to get back the ring request map which contains :servlet-request key, but the value is nil. Is this the right way to do it or do I miss something?

following is the code:

(ns my-app
     (:use noir.request))
(defpage [:get "/app"] []
     (str (ring-request)))

the result:

{:remote-addr "127.0.0.1", :scheme :http, :query-params {}, :session {}, :form-params {}, :multipart-params {}, :servlet #, :request-method :get, :query-string nil, :content-type nil, :cookies {"ring-session" {:value "eb509a65-d33a-40d2-9646-e2ff785428b0"}}, :uri "/app", :server-name "127.0.0.1", :params {}, :headers {"cookie" "ring-session=eb509a65-d33a-40d2-9646-e2ff785428b0", "accept-charset" "GBK,utf-8;q=0.7,*;q=0.3", "accept-language" "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4", "accept-encoding" "gzip,deflate,sdch", "accept" "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "user-agent" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.106 Safari/535.2", "connection" "keep-alive", "host" "127.0.0.1:8080"}, :servlet-request #, :content-length nil, :server-port 8080, :character-encoding nil, :servlet-response #, :body #, :servlet-context #}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你曾走过我的故事 2024-12-20 13:28:53

看起来环请求映射是根据您用于网络服务器的适配器构建的。对于jetty,这发生在ring.adapter.jetty中:

https://github.com/ring-clojure/ring/blob/master/ring-jetty-adapter/src/ring/adapter/jetty.clj

如果我是你,我会创建我自己的适配器,将您需要的内容显式添加到请求映射中。具体来说,您可以替换

request-map (servlet/build-request-map request)

为以下内容:

request-map (assoc (servlet/build-request-map request) :jetty-request request)

It looks like the ring request map is constructed based on the adapter you are using for the webserver. In the case of jetty, this happens in ring.adapter.jetty:

https://github.com/ring-clojure/ring/blob/master/ring-jetty-adapter/src/ring/adapter/jetty.clj

If I were you, I would create my own adapter that explicitly adds what you need to the request map. Specifically you would replace

request-map (servlet/build-request-map request)

with something like:

request-map (assoc (servlet/build-request-map request) :jetty-request request)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文