4

我正在尝试实现请求端点身份验证。为此,我想从请求标头访问 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 方法中获取价值和使用?

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

4

3 回答 3

3

Compojure 对参数有自定义的解构语法(即,与 Clojure 本身不同)。您可以使用关键字绑定整个请求映射: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-26T10:31:22.570 回答
1

Compojure Sweet API 函数[compojure.api.sweet :refer [defroutes GET PUT context]]让我们绑定整个请求或绑定选择标头。在下面的片段中[: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)))
  1. 向量开头:header-params [{x-http-request-id :- X-Http-Request-Id nil}]使请求中“X-HTTP-REQUEST-ID”标头的值可直接作为x-http-request-id.
  2. squiglies{...}使得请求中是否存在 x-http-request-id 标头是可选的。
  3. 这些:- X-Http-Request-Id nil东西给了它一个在其他地方定义的模式,比如(s/defschema X-Http-Request-Id (rss/describe String "Request ID for tracing calls")).

一旦你让那些孩子绑定了名字,你就可以使用这些名字。compojure 的人在记录你在那里可以做的一切方面做得并不好。看看他们的例子,你会发现这样的东西。

于 2017-07-27T04:42:28.447 回答
-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 吗?

于 2017-07-26T11:07:20.100 回答