Implementing a JSON API
Up to this point, we've been focusing on serving browser requests and returning HTML responses. In this section, we'll see how to extend the ElixirDrip application with a JSON API, giving our users the ability to interact with their files outside the browser. To demonstrate how to accomplish this, we'll implement the index action of the file controller, along with the ability to log in via the API. As usual, let's begin by looking at our updated router:
$ cat apps/elixir_drip_web/lib/elixir_drip_web/router.ex defmodule ElixirDripWeb.Router do use ElixirDripWeb, :router # ... pipeline :api do plug(:accepts, ["json"]) end scope "/", ElixirDripWeb do pipe_through(:browser) resources("/files", FileController, only: [:index, :new, :create, :show]) # ... end scope "/api", ElixirDripWeb.Api as: :api do pipe_through(:api) get("/files", FileController, :index) end end
We've created a new pipeline, named :api
, which for...