Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Content Negotiation Tutorial #1

Closed
7 tasks done
nelsonic opened this issue May 12, 2020 · 6 comments
Closed
7 tasks done

Create Content Negotiation Tutorial #1

nelsonic opened this issue May 12, 2020 · 6 comments
Assignees
Labels

Comments

@nelsonic
Copy link
Member

nelsonic commented May 12, 2020

Our goal with the @dwyl App is to have a server-side rendered (and progressively enhanced) Web UI (HTML) that works on any device and a REST API (JSON) that can be consumed programmatically by any script/system/app. We want the default UX (the Web) to be great and we want the API to be available from "Day 1". We definitely don't want to create/maintain two separate Phoenix Apps. We don't even want to maintain two separate router pipelines. We want the same route to transparently return a different representation of the content dependent on the Accepts header.

Todo

  • Create a tutorial that builds an App from scratch that:
    • Returns a random famous quotation on the GET / request.
    • Render HTML if the Accepts header is text/html
    • Render JSON if the Accepts header is application/json

Read and understand the following:

@nelsonic
Copy link
Member Author

Seeing this error:

     ** (ArgumentError) a content-type header is required when setting a binary body in a test connection

Will investigate when I get back to my desk.

@nelsonic
Copy link
Member Author

nelsonic commented May 16, 2020

Derp ... 🤦
Can you spot what is wrong with this code:

defmodule AppWeb.PageControllerTest do
  use AppWeb.ConnCase

  test "GET /", %{conn: conn} do
    conn =
      conn
      |> put_req_header("accept", "text/html")
      |> get(conn, "/")

    assert html_response(conn, 200) =~ "Welcome to Phoenix!"
  end
end

If you can, great! If not, don't worry, it's subtle! See the commit that fixed the test: 7fc61bc

@nelsonic
Copy link
Member Author

image

@nelsonic
Copy link
Member Author

image

@nelsonic
Copy link
Member Author

Terminal JSON on localhost:
enthusiasm-terminal-quote

nelsonic added a commit that referenced this issue May 18, 2020
nelsonic added a commit that referenced this issue May 18, 2020
@nelsonic
Copy link
Member Author

I'm fairly happy with the tutorial: https://github.com/dwyl/phoenix-content-negotiation-tutorial
(feedback and improvements very much welcome!)

Now I'm working on generalising the solution in a Plug so that any App can do this with minimal code: https://github.com/dwyl/content_negotiation_plug

🧑‍💻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant