gpt4 book ai didi

elixir - 参数不包含 Elixir/Phoenix 中的 POST 正文

转载 作者:行者123 更新时间:2023-12-04 10:47:48 25 4
gpt4 key购买 nike

我尝试构建一个非常简单的 REST API。它不包括数据库或模型。

这是我的路由器:

defmodule Zentonies.Router do
use Zentonies.Web, :router

pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
end

pipeline :api do
plug :accepts, ["json"]
end

scope "/v1/events/", Zentonies do
pipe_through :api
post "/call", PageController, :call
end

end

这是 Controller :
defmodule Zentonies.PageController do
require Logger
import Joken
use Zentonies.Web, :controller

def index(conn, _params) do
render conn, "index.html"
end

def call(conn, params) do
Logger.debug inspect(params)
conn
|> put_status(200)
|> text("Response.")
end
end

现在,如果我 HTTP POST 到这个端点, inspect(params)不返回我的 POST 请求的 JSON 正文。相反,它返回 :call .

任何帮助是极大的赞赏!

最佳答案

一个 call/2函数是defined by Phoenix供自己使用,以便在每个 Phoenix Controller 中调度正确的操作。通过创建具有该名称的函数,您将覆盖内置功能。您必须为该操作使用不同的名称。查看 Phoenix.Controller.Pipeline 文档中的“ Controller 是插件”部分:

Controllers are plugs

Like routers, controllers are plugs, but they are wired to dispatch to a particular function which is called an action.

For example, the route:

get "/users/:id", UserController, :show

will invoke UserController as a plug:

UserController.call(conn, :show)

which will trigger the plug pipeline and which will eventually invoke the inner action plug that dispatches to the show/2 function in the UserController.

关于elixir - 参数不包含 Elixir/Phoenix 中的 POST 正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38831903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com