I’m trying to make a simple dictionary app and I can’t get the httpoison get request to go through with the word the user types in the input. Here’s my code:
defmodule LightAppWeb.DictionaryPage do
use LightAppWeb, :live_view
import HTTPoison
def mount(_params, _session, dictionaryStates) do
{:ok, assign(dictionaryStates, word: "")}
end
def make_request(url) do
IO.puts("Request URL: #{url}")
case HTTPoison.get(url) do
{:ok, %{status_code: 200, body: body}} ->
# Handle the successful response
IO.inspect(body)
{:ok, %{status_code: status_code}} ->
# Handle other successful status codes if needed
IO.puts("Received status code: #{status_code}")
{:error, error} ->
# Handle the request error
IO.puts("Request error: #{inspect(error)}")
end
end
def render(assigns) do
~H"""
<p>What word do you want to learn more about?</p>
<input class="border border-2 border-[#000000]" value={@word} />
<button phx-click="request">Submit</button>
"""
end
def handle_event("request", %{"value" => word}, dictionaryStates) do
url = "https://api.dictionaryapi.dev/api/v2/entries/en/#{word}"
make_request(url)
{:noreply, dictionaryStates}
end
end
For example, if the user types “hello” in the input, site would hit the endpoint https://api.dictionaryapi.dev/api/v2/entries/en/ instead of https://api.dictionaryapi.dev/api/v2/entries/en/hello