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

时间:2017-02-28 22:47:56

标签: json elixir phoenix-framework httpoison

在elixir genserver进程中,我正在执行post_metdata方法,以便向Phoenix端点(在另一台服务器上)发送JSON HTTP Patch请求。服务器继续出错,表示没有匹配子句显示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

期望凤凰控制器方法,但不匹配

  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

router.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

我已经使用Insomnia成功执行了模拟JSON数据结构的请求。我向Httpbin发送了两个请求并进行了比较。我能找到的唯一区别是Content-type为HTTPoison发送的请求有重复的application / json条目。但是,我找不到防止重复值的理由或选择。也没有任何理由说凤凰会扼杀它。

1 个答案:

答案 0 :(得分:0)

由于无法解释的原因,HTTPoison正在添加重复的&#39; application / json&#39; content-type标头中的值。插件窒息有多个内容类型值。我通过在传入标题的末尾添加一个分号来解决这个问题。

case HTTPoison.patch webhook, encode(metadata), [{"content-type", "application/json;"}] do

结束分号终止内容类型值。 HTTPoison只插入一个&#39; application / json&#39;值和插头表现正常。

奇怪。