elixir http request post &get

defmodule Gateway.Http do
  def post(conn, url, content) do
    headers = headers(conn)
    case HTTPoison.post(url, content, headers) do
       {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, body}
       {:ok, %HTTPoison.Response{status_code: 404}} ->
        {:error, 404}
    end
  end

  def get(conn, url) do
    headers = headers(conn)
    case HTTPoison.get(url, headers) do
      {:ok, %HTTPoison.Response{status_code: 200, body: body}} ->
        {:ok, body}
      {:ok, %HTTPoison.Response{status_code: 404}} ->
        {:error, 404}
    end
  end

  defp headers(conn) do
    token = conn.assigns.current_user["token"]
    [{"Content-Type", "application/json"}] ++ [{"Authorization", "bearer: " <> token}]
  end
end

 

posted @ 2022-10-08 16:35  孤独信徒  阅读(10)  评论(0编辑  收藏  举报