No function clause matching in Plug.Conn.resp/3

Hi all

When I try to compile phoenix apps I’ve got following error:

1) test GET /odata/InsCharInfoSet (WebServer.OdataControllerTest)
     test/controllers/odata_controller_test.exs:4
     ** (FunctionClauseError) no function clause matching in Plug.Conn.resp/3
     stacktrace:
       (plug) lib/plug/conn.ex:493: Plug.Conn.resp(%Plug.Conn{adapter: {Plug.Adapters.Test.Conn, :...}, assigns: %{}, before_send: [#Function<1.24097251/1 in Plug.Logger.call/2>], body_params: %{}, cookies: %Plug.Conn.Unfetched{aspect: :cookies}, halted: false, host: "www.example.com", method: "GET", owner: #PID<0.317.0>, params: %{"uri" => "InsCharInfoSet"}, path_info: ["odata", "InsCharInfoSet"], path_params: %{}, peer: {{127, 0, 0, 1}, 111317}, port: 80, private: %{WebServer.Router => {[], %{}}, :phoenix_action => :get, :phoenix_controller => WebServer.OdataController, :phoenix_endpoint => WebServer.Endpoint, :phoenix_format => "json", :phoenix_layout => {WebServer.LayoutView, :app}, :phoenix_pipelines => [:api], :phoenix_recycled => true, :phoenix_route => #Function<3.77350095/1 in WebServer.Router.match_route/4>, :phoenix_router => WebServer.Router, :phoenix_view => WebServer.OdataView, :plug_session_fetch => #Function<1.131660147/1 in Plug.Session.fetch_session/1>, :plug_skip_csrf_protection => true}, query_params: %{}, query_string: "", remote_ip: {127, 0, 0, 1}, req_cookies: %Plug.Conn.Unfetched{aspect: :cookies}, req_headers: [{"accept", "application/json"}], request_path: "/odata/InsCharInfoSet", resp_body: nil, resp_cookies: %{}, resp_headers: [{"cache-control", "max-age=0, private, must-revalidate"}, {"x-request-id", "hkqkpsl2ll9n8imtog39nr15lq3apv7k"}, {"content-type", "application/json; charset=utf-8"}], scheme: :http, script_name: [], secret_key_base: "nj4laNW5zb64xz0blgG5mTUUTkRaBKPoX4no2VGM6lEdHYHSO1qe4CNepeEbj3yS", state: :unset, status: nil}, 200, {"id: 1"})
       (plug) lib/plug/conn.ex:483: Plug.Conn.send_resp/3
       (web_server) web/controllers/odata_controller.ex:1: WebServer.OdataController.action/2
       (web_server) web/controllers/odata_controller.ex:1: WebServer.OdataController.phoenix_controller_pipeline/2
       (web_server) lib/web_server/endpoint.ex:1: WebServer.Endpoint.instrument/4
       (web_server) lib/phoenix/router.ex:261: WebServer.Router.dispatch/2
       (web_server) web/router.ex:1: WebServer.Router.do_call/2
       (web_server) lib/web_server/endpoint.ex:1: WebServer.Endpoint.phoenix_pipeline/1
       (web_server) lib/web_server/endpoint.ex:1: WebServer.Endpoint.call/2
       (phoenix) lib/phoenix/test/conn_test.ex:224: Phoenix.ConnTest.dispatch/5
       test/controllers/odata_controller_test.exs:8: (test)

the action is:

defmodule WebServer.OdataController do

  use WebServer.Web, :controller
  alias SapOdataService.Protocols.Odata
  alias SapOdataService.Odata.Query

  plug WebServer.JsonPlug, %{"content-type": "application/json"}

  def get(conn, %{"uri" => uri}) do

    conn
    |> send_resp(200, {"id: 1"})

  end

end

What am I doing wrong?

Thanks

2 Likes

If you want to respond with JSON you could use json conn, %{status: true}

5 Likes

Yes. If you want to use send_resp for some reason, you need to send :iodata.

@kostonstyle always look at the documentation, it gives some good hints what you are doing wrong. In your case if you really want to use send_resp/3 instead of json, you need to have third parameter type as iodata, and you have a Map.

https://hexdocs.pm/plug/Plug.Conn.html#send_resp/3 and click on the “body” that’d get you to annotation that this should be :iodata
https://hexdocs.pm/plug/Plug.Conn.html#t:body/0

But stick with json function unless you have a reason.

4 Likes

Because I did not know json function :slight_smile:

1 Like