Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 没有匹配的操作子句来处理HTTPoison.patch上的请求错误_Json_Elixir_Phoenix Framework_Httpoison - Fatal编程技术网

Json 没有匹配的操作子句来处理HTTPoison.patch上的请求错误

Json 没有匹配的操作子句来处理HTTPoison.patch上的请求错误,json,elixir,phoenix-framework,httpoison,Json,Elixir,Phoenix Framework,Httpoison,在elixir genserver进程中,我正在执行post_metdata方法向Phoenix端点(另一台服务器上)发送JSON HTTP补丁请求。服务器继续出错,表示基于方法的参数中没有包含显示JSON链接结构的匹配子句。包括id,但不包括传递的数据有效负载。有没有关于问题所在的建议 客户端代码 defp post_metadata(metadata, webhook) do case HTTPoison.patch webhook, encode(metadata), [{"con

在elixir genserver进程中,我正在执行post_metdata方法向Phoenix端点(另一台服务器上)发送JSON HTTP补丁请求。服务器继续出错,表示基于方法的参数中没有包含显示JSON链接结构的匹配子句。包括id,但不包括传递的数据有效负载。有没有关于问题所在的建议

客户端代码

defp post_metadata(metadata, webhook) do
    case HTTPoison.patch webhook, encode(metadata), [{"content-type", "application/json"}] do
      {:ok, %HTTPoison.Response{status_code: 200} = response} ->
        # Logger.debug response.body
        Logger.debug "Successfully extracted and posted metadata for #{webhook}"
      {:error, %HTTPoison.Error{reason: reason}} ->
        Logger.warn "Unable to extract and post metadata for #{webhook}"
    end

  end

  defp encode(metadata) do
    %{
      "link":
      %{
        "title": metadata.title,
        "description": metadata.description
      }
    }
    |> Poison.encode!
  end
应为Phoenix控制器方法,但未匹配

  def update(conn, %{"id" => id, "link" => link_params}) do
    link = Repo.get!(Link, id)
    changeset = Link.changeset(link, link_params)

    case Repo.update(changeset) do
      {:ok, link} ->
        render(conn, "show.json", link: link)
      {:error, changeset} ->
        conn
        |> put_status(:unprocessable_entity)
        |> render(Mini.ChangesetView, "error.json", changeset: changeset)
    end
  end
路由器.ex

defmodule Mini.Router do
  use Mini.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"]
    plug Plug.Logger, log: :debug
  end

  scope "/", Mini do
    pipe_through :browser # Use the default browser stack

    get "/", PageController, :index
  end

  scope "/api", Mini do
    pipe_through :api
    resources "/links", LinkController, except: [:new, :edit]
  end
end
将错误记录到控制台

[debug] ** (Phoenix.ActionClauseError) bad request to Mini.LinkController.update, no matching action clause to process request
    (mini) web/controllers/link_controller.ex:39: Mini.LinkController.update(%Plug.Conn{adapter: {Plug.Adapters.Cowboy.Conn, :...}, assigns: %{}, before_send: [#Function<1.42492691/1 in Plug.Logger.call/2>, #Function<1.42492691/1 in Plug.Logger.call/2>, #Function<0.111727833/1 in Phoenix.LiveReloader.before_send_inject_reloader/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "localhost", method: "PATCH", owner: #PID<0.438.0>, params: %{"id" => "12"}, path_info: ["api", "links", "12"], path_params: %{}, peer: {{127, 0, 0, 1}, 55369}, port: 4000, private: %{Mini.Router => {[], %{}}, :phoenix_action => :update, :phoenix_controller => Mini.LinkController, :phoenix_endpoint => Mini.Endpoint, :phoenix_format => "json", :phoenix_layout => {Mini.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_route => #Function<4.107513407/1 in Mini.Router.match_route/4>, :phoenix_router => Mini.Router, :phoenix_view => Mini.LinkView, :plug_session_fetch => #Function<1.61377594/1 in Plug.Session.fetch_session/1>}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"content-type", "application/json, application/json"}, {"user-agent", "hackney/1.6.6"}, {"host", "localhost:4000"}, {"content-length", "58"}], request_path: "/api/links/12", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "5bggaasurlj1oe027nvmv5aiek0hq3k8"}], scheme: :http, script_name: [], secret_key_base: "YfnqjmBhsSJMF/TmhK6qpMnJl7mS0tIYHk1tZ/dZUA6d7KOdv2g/AOJUfWo8sulb", state: :unset, status: nil}, %{"id" => "12"})
    (mini) web/controllers/link_controller.ex:1: Mini.LinkController.action/2
    (mini) web/controllers/link_controller.ex:1: Mini.LinkController.phoenix_controller_pipeline/2
    (mini) lib/mini/endpoint.ex:1: Mini.Endpoint.instrument/4
    (mini) lib/phoenix/router.ex:261: Mini.Router.dispatch/2
    (mini) web/router.ex:1: Mini.Router.do_call/2
    (mini) lib/mini/endpoint.ex:1: Mini.Endpoint.phoenix_pipeline/1
    (mini) lib/plug/debugger.ex:123: Mini.Endpoint."call (overridable 3)"/2
    (mini) lib/mini/endpoint.ex:1: Mini.Endpoint.call/2
    (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
    (cowboy) /Users/billc/dev/mini/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4
[debug]**(Phoenix.ActionClauseError)对Mini.LinkController.update的请求不正确,没有与处理请求匹配的操作子句
(mini)web/controllers/link_controller.ex:39:mini.LinkController.update(%Plug.Conn{adapter:{Plug.Adapters.Cowboy.Conn,:…},赋值:%{},在发送之前:[#函数,#函数,#函数],体参数:%{},cookies:%Plug.Conn.Unfetched{aspect::cookies},暂停:false,主机:“localhost”,方法:“补丁”,所有者:#PID,参数:%%“id=”12},path_info:[“api”,“links”,“12”],path_参数:%{},peer:{127,0,1},55369},端口:4000,private:%{Mini.Router=>{[],%{},:phoenix_action=>:update,:phoenix_controller=>Mini.LinkController,:phoenix_endpoint=>Mini.endpoint,:phoenix_format=>“json”,:phoenix_布局=>{Mini.LayoutView,:app,:phoenix_管道=>[:api],:phoenix_route=>#Function,:phoenix_router=>Mini.router,:phoenix_view=>Mini.LinkView,:plug_session_fetch=>#Function},query_参数:%{},query_字符串:,,远程_ip:{127,0,0,1},请求cookies:%plug.Conn.Unfetched{aspect::cookies},请求头:[{“内容类型”,“应用程序/json”,“应用程序/json”},,“用户代理”,“hackney/1.6.6},{“主机”,“本地主机:4000”},{“内容长度”,“58”}],请求路径:“/api/links/12”,响应体:nil,响应cookie:%{},响应头:[{“缓存控制”,“最大年龄=0,私有,必须重新验证”},{“x-request-id”,“5BGAAURLJ1OE027NVMV5AIEK0HQ3K8}],方案::http,脚本名称:[],密钥库:“yfnqjmbhsjmf/TmhK6qpMnJl7mS0tIYHk1tZ/dZUA6d7KOdv2g/AOJUfWo8sulb”,状态::未设置,状态:nil},%{“id”=>“12”})
(mini)web/controllers/link_controller.ex:1:mini.LinkController.action/2
(mini)web/controllers/link\u controller.ex:1:mini.LinkController.phoenix\u controller\u管道/2
(mini)lib/mini/endpoint.ex:1:mini.endpoint.instrument/4
(mini)lib/phoenix/router.ex:261:mini.router.dispatch/2
(mini)web/router.ex:1:mini.router.do_call/2
(mini)lib/mini/endpoint.ex:1:mini.endpoint.phoenix_管道/1
(mini)lib/plug/debugger.ex:123:mini.Endpoint.“调用(可重写3)”/2
(mini)lib/mini/endpoint.ex:1:mini.endpoint.call/2
(plug)lib/plug/adapters/cowboy/handler.ex:15:plug.adapters.cowboy.handler.upgrade/4
(cowboy)/Users/billc/dev/mini/deps/cowboy/src/cowboy_protocol.erl:442::cowboy_protocol.execute/4

我成功地执行了一个模拟JSON数据结构的请求。我将两个请求都发送到Httpbin并进行了比较。我能找到的唯一区别是内容类型对于HTTPoison发送的请求具有重复的application/JSON条目。但是,我找不到防止重复值的原因或选项。也找不到任何原因Phoenix会窒息的。

由于一个无法解释的原因,HTTPoison正在内容类型头中添加重复的“application/json”值。Plug因为有多个内容类型值而窒息。我通过在传入头的末尾添加分号来解决这个问题

case HTTPoison.patch webhook, encode(metadata), [{"content-type", "application/json;"}] do
结束分号终止内容类型值。HTTPoison只插入一个“application/json”值,插件正常运行


奇怪。

我认为无效的内容类型导致插件无法解析正文,但我不知道为什么会这样发送标题。我同样怀疑内容类型。找到了错误报告。它已在hackney 1.7.0中修复。非常好。感谢跟踪!