Open WLPhoenix opened 9 years ago
Here's how I do a post request:
13 body = "code=#{code}&grant_type=authorization_code&client_secret=CDRPVW63YBXLHSPGL4Q25AFJKTXC7EYWOJ6LOQEE3UDIOABT6N&client_id=7EBFEB24BRVOV6J5KG"
14 case HTTPoison.post("https://www.eventbrite.com/oauth/token", body, [{<<"Content-Type">>, <<"application/x-www-form-urlencoded">>}]) do
15 {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
16 IO.puts "BODY :: [[#{body}]]"
17 {:ok, %HTTPoison.Response{status_code: 404, body: body}} ->
18 IO.puts "Not found :-( #{body}"
19 {:ok, %HTTPoison.Response{status_code: status_code, body: body}} ->
20 IO.puts "I got a different code #{status_code} -- what the hell? #{body}"
21 {:error, %HTTPoison.Error{reason: reason}} ->
22 IO.inspect reason
23 end
Added a new route like this:
9 get "/auth", CodeForConduct.PageController, :auth, as: :pages
When I want different handling based on query params, I can do it like this:
# when there is a "code" query param
def auth(conn, %{"code" => code}) do
...
end
# otherwise
def auth(conn, _params) do
...
end
Both of these are the same method, and they pattern match on the incoming parameters to select which body to execute.
How to do a redirect from a controller function:
29 redirect conn, "https://www.eventbrite.com/oauth/authorize?response_type=code&client_id=7EBFEB24BRVOV6J5KG"
# Where body is a map
case Poison.encode body do
{:ok, json} -> HTTPoison.post('http://localhost:3300', json)
{:error, r} -> IO.puts r
end
# How to get environment variable, and default if not found
[port: System.get_env("PORT") || 4000]
To be more specific:
put_session(conn, "key", "value")
is NOT the same as:
conn = put_session(conn, "key", "value")
Use the second one, otherwise you just threw away your session value.
http://gogogarrett.sexy/programming-in-elixir-with-the-phoenix-framework-building-a-basic-CRUD-app/