如何:body从compojure 安全地获取InputStream 的内容?
我正在尝试使用Friend使用我的环路径进行身份验证,compojure handler/site但是当我尝试:body从http POST请求(这是一个Java InputStream)读取时,它已关闭:
23:01:20.505 ERROR [io.undertow.request] (XNIO-1 task-3) Undertow request failed HttpServerExchange{ POST /paypal/ipn}
java.io.IOException: UT000034: Stream is closed
at io.undertow.io.UndertowInputStream.read(UndertowInputStream.java:84) ~[undertow-core-1.1.0.Final.jar:1.1.0.Final]
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[na:1.8.0_45]
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[na:1.8.0_45]
at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[na:1.8.0_45]
at java.io.BufferedReader.fill(BufferedReader.java:161) ~[na:1.8.0_45]
at java.io.BufferedReader.read(BufferedReader.java:182) ~[na:1.8.0_45]
at clojure.core$slurp.doInvoke(core.clj:6650) ~[clojure-1.7.0-beta1.jar:na]
at clojure.lang.RestFn.invoke(RestFn.java:410) ~[clojure-1.7.0-beta1.jar:na]
Run Code Online (Sandbox Code Playgroud)
如果我删除处理程序,问题就会消失.我找到了一个名为groundhog的可能解决方案,它捕获并存储所有请求.我正在使用的库,clojure-paypal-ipn最初reset在流上调用,但是Undertow(或者其他几个Java/Clojure服务器)不支持它,所以我解决了它.
这是与compojure的作者weavejester的相关讨论.
以下是我的代码的一些片段:
(defroutes routes
...
(POST "/paypal/ipn" [] (payment/paypal-ipn-handler
payment/paypal-data …Run Code Online (Sandbox Code Playgroud) 如何列出处理函数上的所有路由?我正在寻找类似于 Rails' 的行为rake routes。例如:
(defroutes foo-routes
(GET "/foo/:foo-id"
[foo-id]
"bar response")
(GET "/bar/:bar-id"
[bar-id]
"foo response"))
Run Code Online (Sandbox Code Playgroud)
那么是否可以从 foo-bar-routes 中提取包含以下内容的地图?
{:GET "/foo/:foo-id"
:GET "/bar/:bar-id"}
Run Code Online (Sandbox Code Playgroud) 大约 10 多年前,我在适度涉足 clojure 后又回到了它,所以我可能在这里做了一些愚蠢的事情。
compojure我正在尝试使用服务器编写一个简单的 API ring,现在我已将问题隔离为几行。wrap-json-body我有一条路线和一个处理程序,并且我已按照文档中的建议包装了我的处理程序ring-json。
我的handler.clj是这样的:
(defroutes app-routes
(PUT "/item/:id" {{id :id} :params body :body} (str id ", " body))
(route/not-found "Not Found"))
(def app
(-> app-routes
(middleware/wrap-json-body)
(middleware/wrap-json-response)))
Run Code Online (Sandbox Code Playgroud)
这应该很简单,我可以将 clojure 数据返回为 json OK。问题是当我尝试读取PUT请求正文 json 时。
$ curl -XPUT -H "Content-type: application/json" -d '{ "id": 32, "name": "pad" }' 'localhost:3001/item/5'
5, {"id" 32, "name" "pad"}
Run Code Online (Sandbox Code Playgroud)
我希望body充满{:id 32 :name "pad"}
这是整个请求对象:
; (PUT …Run Code Online (Sandbox Code Playgroud) 我有一个小的compojure站点,其路由定义如下:
(defroutes example
(GET "/" [] {:status 200
:headers {"Content-Type" "text/html"}
:body (home)})
(GET "/*" (or (serve-file (params :*)) :next))
(GET "/execute/" [] {:status 200
:headers {"Content-Type" "text/html"}
:body (execute-changes)})
(GET "/status/" [] {:status 200
:headers {"Content-Type" "text/html"}
:body (status)})
(route/not-found "Page not found"))
Run Code Online (Sandbox Code Playgroud)
当我尝试加载项目时,我收到此错误:
java.lang.Exception: Unsupported binding form: (or (serve-file (params :*)) :next)
我究竟做错了什么?我从互联网上分散的例子中获取了大部分内容.
添加空向量后,我收到此错误:
java.lang.Exception: Unable to resolve symbol: serve-file in this context
我的代码:
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defroutes checkin-app-handler
(GET "/:code" [code & more] (json-response {"code" code "params" more})))
Run Code Online (Sandbox Code Playgroud)
当我将文件加载到repl并运行此命令时,params似乎是空白的:
$ (checkin-app-handler {:server-port 8080 :server-name "127.0.0.1" :remote-addr "127.0.0.1" :uri "/123" :query-string "foo=1&bar=2" :scheme :http :headers {} :request-method :get})
> {:status 200, :headers {"Content-Type" "application/json"}, :body "{\"code\":\"123\",\"params\":{}}"}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我需要得到查询字符串,但是params地图总是空的..
这个复杂的问题是双重的!
谢谢!
你能建议我从字符串序列中提取子串的最短和最简单的方法吗?我从使用enlive框架获取此集合,该框架从某些网页获取内容,这是我得到的结果:
("background-image:url('http://s3.mangareader.net/cover/gantz/gantz-r0.jpg')"
"background-image:url('http://s3.mangareader.net/cover/deadman-wonderland/deadman-wonderland-r0.jpg')"
"background-image:url('http://s3.mangareader.net/cover/12-prince/12-prince-r1.jpg')" )
Run Code Online (Sandbox Code Playgroud)
我想要的是从序列中的每个字符串中提取URL来获得一些帮助.我尝试使用分区函数,但没有成功.任何人都可以为此问题提出正则表达式或任何其他方法吗?
谢谢
使用有什么区别 -
(route/files "/" {:root "yeoman/app"})
(route/resources "/" {:root "yeoman/app"})
Run Code Online (Sandbox Code Playgroud)
用于设置静态文件夹的根目录?
谢谢,穆尔塔扎
我正在尝试创建一个具有参数的路由,该参数在compojure中包含逗号
(GET "/tags/multiple/:tag-names" [tag-names] multiple-tags)
Run Code Online (Sandbox Code Playgroud)
但出于某种原因,每当我在:tag-names字段中包含一个逗号时,它就是404s.没有逗号时它可以正常工作.
有谁知道是什么原因造成的,以及我如何解决这个问题?
不确定为什么当我lein with-profile +live ring uberjar再使用java -jar我的uberjar时,出现此异常:java.lang.NoClassDefFoundError: clojure/lang/Var。
project.clj:
(defproject gn-preview-api "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.9.0"]]
:main gn-preview-api.www.app
:target-path "target/%s"
:profiles {:uberjar {:aot :all}
:staging {:aot :all}
:live {:aot :all}
:dev {:plugins [[lein-ring "0.9.7"]]
:dependencies [[javax.servlet/servlet-api "2.5"]]}})
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?