Clojure,Compojure-api : Access Request headers

标签 clojure ring compojure-api

我正在尝试实现请求端点身份验证。为此,我想从请求 header 访问 accessToken 值。

我的 GET 请求终点是

curl 命令

curl -X GET \
  'http://localhost:3000/hello?id=10' \
  -H 'accesskey: 23423sfsdfsdfsfg' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: f69b34e6-4888-ec31-5fbc-b734e176571b' \
  -d '{
    "artwork": {id" : 1}
}'

HTTP 命令
GET /hello?id=10 HTTP/1.1
Host: localhost:3000
Content-Type: application/json
accessKey: 23423sfsdfsdfsfg
Cache-Control: no-cache
Postman-Token: b974719d-5e1d-4d68-e910-e9ca50562b2f

我的 GET 方法实现代码
(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id]
    (log/info "Function begins from here")
    (def artworkData (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong)))
    (def data (if (not-empty artworkData)
               {:data artworkData :status 200}
               {:data [] :status 201}))
   (ok data)))

我要获取 accessKey: 23423sfsdfsdfsfg从请求头。

有什么方法可以在我的 GET 方法中获取值(value)和使用?

我正在使用 POSTMAN 来测试所有 API 端点。

最佳答案

Compojure 对参数有自定义解构语法(即与 Clojure 本身不同)。您可以bind the whole request map使用关键字:as

(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id :as request]

如果您只需要请求 header ,则以下内容应该有效
(defapi app
  (GET ["/hello/:id", :id #"[0-9]+" ] [id :as {:headers headers}]

请注意,这仍然允许您绑定(bind)路径参数 id .

关于Clojure,Compojure-api : Access Request headers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45323363/

相关文章:

clojure - 这一行 Clojure 代码的作用是什么?

clojure - Leiningen 中是否有独立的 Clojure 包?

clojure - Ring 和 Pedestal 之间有什么关系 - 我是否需要了解 Ring 才能使用 Pedestal?

pdf - 如何使用ring提供pdf流服务

clojure - lein ring uberjar -- java.lang.NoClassDefFoundError : clojure/lang/Var

unit-testing - 如何使用 `with-redefs` 始终从函数中抛出异常

java - 学习 Java,以便我可以使用 clojure

Clojure #'是什么意思

clojure - 为什么 compojure 路由定义为宏?