CALEB KPABITEY
CALEB KPABITEY

Reputation: 83

How do I configure phoenix /elixir apps to accept https requests

I have created some phoenix apps and all seem to by default not accept https requests. I get the error [error] Cowboy returned 400 and there are no headers in the connection.. Http requests return data as expected.

Upvotes: 1

Views: 1289

Answers (1)

skwidbreth
skwidbreth

Reputation: 8404

I believe that you need to configure your app's endpoint in the relevant config.exs - something like:

config :my_app, MyApp.Endpoint,
  http: [port: {:system, "PORT"}],
  url: [scheme: "https", host: System.get_env("HOST"), port: 443],
  force_ssl: [rewrite_on: [:x_forwarded_proto]]

where System.get_env("HOST") is your app's url... localhost:4000, in the case of local development, I'd think.

Documentation - http://wsmoak.net/phoenix/Phoenix.Endpoint.html

Then you'll need to add the CORS plug - https://github.com/mschae/cors_plug

I built a Phoenix app to act as an API, and I followed the doc's instructions, setting it up in my router like they did, more or less:

pipeline :api do
  plug CORSPlug, [origin: Application.get_env(:my_app, :client_url)]
  plug :accepts, ["json"]
end


scope "/api", PhoenixApp do
  pipe_through :api

  resources "/articles", ArticleController
  options   "/articles", ArticleController, :options
  options   "/articles/:id", ArticleController, :options
end

Upvotes: 1

Related Questions