我正在尝试实现请求端点身份验证。为此,我希望从请求头访问accessToken值。
我的GET请求端点是
卷曲命令
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方法中得到值和使用吗?
我正在使用邮递员来测试所有API端点。
发布于 2017-07-26 10:31:22
Compojure对参数具有自定义的析构语法(即与Clojure正确的语法不同)。您可以使用关键字bind the whole :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。
发布于 2017-07-27 04:42:28
Compojure Sweet函数(如[compojure.api.sweet :refer [defroutes GET PUT context]] )让我们绑定整个请求或绑定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-请求-id标头在请求中可选。:- X-Http-Request-Id nil为它提供了一个模式,它是在其他地方定义的,比如(s/defschema X-Http-Request-Id (rss/describe String "Request ID for tracing calls"))。一旦你把那些孩子绑在名字上,你就可以用这些名字了。法律上的人在记录你能做的一切方面做得不太好。仔细看看他们的例子,你会发现这样的东西。
发布于 2017-07-26 11:07:20
我已经想出了解决这个问题的办法。请检查这里的解决方案。
(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})))我不认为这是明智之举。
你能帮我查一下我的解决方案吗?你能告诉我还有别的方法可以获得访问密钥吗?
https://stackoverflow.com/questions/45323363
复制相似问题