首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >clojure: PUT to server总是返回404。

clojure: PUT to server总是返回404。
EN

Stack Overflow用户
提问于 2022-06-17 09:15:05
回答 1查看 60关注 0票数 0

我有一个服务器承载我的API。我的API依赖于第三方API (Spotify)请求的数据。下面是我的API处理程序的相关部分:

代码语言:javascript
运行
复制
(ns myapp.api.handler
  (:require
    [compojure.api.sweet :refer :all]
    [ring.util.http-response :refer [ok forbidden no-content not-found bad-request]]
    [clj-spotify.core :as spotify]))

(defroutes api-routes
  (api
    {:middleware [wrap-api]
     :swagger {:ui "/api-docs"
               :spec "/swagger.json"
               :data {:info {:title "My API"
                             :description "A description for My API"}
                      :consumes ["application/json"]
                      :produces ["application/json"]}}}
    
    (context "/api" []
      (context "/me" []
        (PUT "/player" []
          :query-params [device_id :- String]
          (handle-player-put device_id))))))

从我的路由处理程序中可以看出,我基本上希望将第三方API的响应转发给我的API。下面是处理程序函数,handle-player-put

代码语言:javascript
运行
复制
(defn handle-player-put [device-id]
  (let [available-devices (-> (spotify/get-current-users-available-devices
                                {}
                                (lm/oauth-token :spotify))
                              :devices)]
    (doseq [device available-devices]
      (when (= (:id device) device-id)
        (if (not (:is_restricted device))
          (let [response (spotify/transfer-current-users-playback
                           {:device_ids [device-id]
                            :play false}
                           (lm/oauth-token :spotify))]
            (case (-> response :error :status)
              nil (no-content)
              404 (do
                    (println "Playback response: 404")
                    (not-found "Spotify could not find the requested resource."))
              {:status (-> response :error :status)
               :headers {}
               :body (-> response :error :message)})))))))

在成功的(spotify/transfer-current-users-playback)请求之后,response绑定到{}。错误后的response示例类似于{:error {:status 502, :message "Bad gateway."}}

无论transfer-current-users-playback是否成功,我总是得到一个404错误(使用正文文本Not Found [404])。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2022-06-19 06:46:46

doseq总是返回nil,所以您的处理程序返回nil --它被compojure解释为“此处理程序将不处理请求;跳到下一个处理程序”,如果没有其他处理程序处理该请求,则会得到404 not。

不应使用(doseq…)(当…(如果您需要返回expr

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72657035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档