How do I return a random response from HTTPoison

Im fetching an api

    with {:ok, %{body: body}} <- HTTPoison.get(url, headers) do
      Poison.decode!(body)
    end

Tho, this always returns one response, never something else. How can I randomize the response?
I tried it with Enum.random(Poison.decode!(body)) but it didnt fix the issue

I don’t see an issue. You get one object containing a response.

Your question is vague. Please post what response you’re seeing and what part of it would you want returned.

1 Like

I get the following response
“Name is Jill”, tho there are thousands of names in that api. When I run the function 10x, its always returning “Name is Jill”, I want to return a random name.

We don’t have any context and you still didn’t give us any usable info except what you expect.

Have you tried calling the same API with curl, Postman etc.? Does it show different results?

If the API always gives you the same reply, there is nothing you can randomize.

Can you please tell more about the request you are doing? Perhaps you need to change that.

Or tell us how exactly the response looks like.

%{
    "name" => "...",
    "equipment" => "...",
    "type" => "..."
  }

this is the response. When I run the fetch function a few time, its always returning a different response. I made a sub function (above) that only returns the name of that map. However, if I run that function a few times, its always the same like “Jill”, “Jill” …

Which fetch function?

There is no such concept of a sub function in elixir.

So can you please elaborate how the request looks like exactly?

And what API you are talking against?

Which fetch function?
The fetchData function I posted above.

There is no such concept of a sub function in elixir.

I made a separate function, that calls the fetch function and then just filters it down to the name.
And the problem is, that its returning the same thing over and over again, so I somehow have to Enum.random this, but idk where and how …

I can’t follow you.

You say calling the fetchData returns different things each time, the you say it returns the same thing every time.

Can you please share request details?

When I run the fetchData function, its returning a map with all the data, its a direct api request.
Yesterday I thought “I only need the name of that map”, so I “filtered” it down to only print the name of the map. Now I dont want to have the same results over and over, I want to randomize the name.

Can you please share request details?

No.

There is still no hint about the url and headers you use.

yes sure then

The API endpoint returns a list of exercises, not a single object as you sayd.

body
|> Poison.decode!()
|> Enum.random()

This should return a random individual item from that list. if this does not work, please tell us what you got instead. And do not just say “did not work” or “gave an error” but tell us the exact value or error you saw.

2 Likes

I already tried this. ** (BadMapError) expected a map

Please tell us the full error.

I doubt that this is raised by Enum.random/1, but instead by something else.

1 Like

if I remove the enum.random, everything works fine. everything else also works fine.

def getName do
    case APi.fetchData() do
      [%{"name" => name} | _] -> name
      _ -> {:error, :invalid_response}
    end
  end

returning “invalid response” when doing

 body
      |> Poison.decode!()
      |> Enum.random()

If I remove that, its returning the response as usual

  1. Is this what caused the BadMapError?
  2. I assume that body |> Poison.decode!() |> Enum.random() is what gets returned from APi.fetchData/0, so [%{"name" => name} | _] can not match in the case, as Enum.random/0 returns a single random item from the enumerable passed to it. You need to change the pattern to %{"name" => name}.
2 Likes

Oh, and to make one thing clear!

Enum.random/1 operates on what already has been returned by HTTPoison it does not change the request itself.

At the time you call Enum.random/1 the request already has happened and also Poison.decode/1 has already decoded the returned body from JSON to equivalent Elixir data structures.