我正在尝试实现请求端点身份验证。为此,我想从请求标题中访问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方法中获取价值和使用?
我正在使用POSTMAN测试所有API端点。
答案 0 :(得分:3)
Compojure为参数提供了自定义解构语法(即,与Clojure不同)。您可以bind the whole request map使用关键字:as
(defapi app
(GET ["/hello/:id", :id #"[0-9]+" ] [id :as request]
如果您只想要请求标头,则以下内容应该有效
(defapi app
(GET ["/hello/:id", :id #"[0-9]+" ] [id :as {:headers headers}]
请注意,这仍然允许您绑定路径参数id
。
答案 1 :(得分:1)
像[compojure.api.sweet :refer [defroutes GET PUT context]]
这样的Compojure Sweet API函数让我们绑定整个请求或绑定select头。在[:as request]
下方的代码段中,我可以使用整个请求。
(GET
"/download/:id"
[:as request]
:header-params [{x-http-request-id :- X-Http-Request-Id nil}]
:path-params [id :- (describe String "The encoded id of the image")]
:summary "Download the image bytes"
:description "This endpoint responds 307 - Temporary Redirect to a cacheable presigned S3 URL for the actual bytes."
(let [http-response (->> request
walk/keywordize-keys
util/extract-base-url
(transform/generate-resource-url (util/decode-key id))
status/temporary-redirect)
expire-time (-> 3 hours from-now coerce/to-date ring-time/format-date)]
(log/infof "x-http-request-id is %s" x-http-request-id)
(response/header http-response "Expires" expire-time)))
:header-params [{x-http-request-id :- X-Http-Request-Id nil}]
开头的向量生成" X-HTTP-REQUEST-ID"的值。我的函数可以直接将x-http-request-id
。{...}
使x-http-request-id标头在请求中可选。:- X-Http-Request-Id nil
内容为其提供了一个Schema,在其他地方定义了(s/defschema X-Http-Request-Id (rss/describe String "Request ID for tracing calls"))
。一旦你将这些孩子绑定到名字,你就可以使用这些名字。这些组合人员在记录你可以做的所有事情方面做得不好。围绕他们的例子,你会发现这样的东西。
答案 2 :(得分:-1)
我已经找到了问题的解决方案。请在这里查看解决方案。
(ns clojure-dauble-business-api.core
(:require [compojure.api.sweet :refer :all]
[ring.util.http-response :refer :all]
[clojure-dauble-business-api.logic :as logic]
[clojure.tools.logging :as log]
[clojure-dauble-business-api.domain.artwork]
[cheshire.core :as json])
(:import [clojure_dauble_business_api.domain.artwork Artwork]))
(defapi app
(GET ["/hello/:id", :id #"[0-9]+"] [id :as request]
(log/info "Function begins from here" request)
(def jsonString (json/generate-string (get-in request [:headers])))
(log/info "Create - Access Key is " (get-in (json/parse-string jsonString true) [:accesskey]))
(def artworkData (logic/artwork-id (->> id (re-find #"\d+") Long/parseLong)))
(def data (if (not-empty artworkData)
{:data artworkData :status 200}
{:data [] :status 201})))
我不认为这是聪明的方式。
你能不能看看我的解决方案并告诉我是否有另一种获取accesskey的方法?