No route found for OPTIONS

Hey guys, I’m having this probably CORS related problem: every time I make a request from the front-end to my api I always have to add options request handler to my router even though I’m doing post request, when I add post handler without options handler I get this error:

[info] OPTIONS /api/sign_up
[debug] ** (Phoenix.Router.NoRouteError) no route found for OPTIONS /api/sign_up (UserRestfulapiPhxWeb.Router)
(user_restfulapi_phx) lib/user_restfulapi_phx_web/router.ex:1: UserRestfulapiPhxWeb.Router.__match_route__/4
(user_restfulapi_phx) lib/phoenix/router.ex:307: UserRestfulapiPhxWeb.Router.call/2
(user_restfulapi_phx) lib/user_restfulapi_phx_web/endpoint.ex:1: UserRestfulapiPhxWeb.Endpoint.plug_builder_call/2
(user_restfulapi_phx) lib/plug/debugger.ex:122: UserRestfulapiPhxWeb.Endpoint."call (overridable 3)"/2
(user_restfulapi_phx) lib/user_restfulapi_phx_web/endpoint.ex:1: UserRestfulapiPhxWeb.Endpoint.call/2
(plug) lib/plug/adapters/cowboy/handler.ex:16: Plug.Adapters.Cowboy.Handler.upgrade/4
(cowboy) /Users/admin/Documents/projects/user_restfulapi_phx/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4

Tried using CORSPlug dep - didn’t help

  pipeline :api do
    plug(CORSPlug, origin: "*")
    plug(:accepts, ["json"])
  end

my request handlers:

options("/sign_up", UserController, :create)
post("/sign_up", UserController, :create)
options("/sign_in", UserController, :sign_in)
post("/sign_in", UserController, :sign_in)

Any help is appreciated!

2 Likes

Did you manage to solve it because I am having similar issues

Is the scope for these /api?

I fixed this issue by putting CORSPlug in my endpoint.exs

plug CORSPlug
Plug AppWeb.Router

see: https://github.com/mschae/cors_plug

4 Likes