Elixir HTTPoisin.get avoid dedup in response data

I am making call to HTTP Rest API in elixir

url = "http://localhost:8080/getScoreData"
   case HTTPoison.get(url) do
     {:ok, %{status_code: 200, body: body}} ->
       Logger.info("body is #{inspect(body)}")
       overall_score = Jason.decode!(body, as: [%OverallScore{}])
       {:ok, overall_score}
   end

which in the web browser returns

{
      "avgPass": 85.55,
      "avgFail": 14.45,
      "totalStudents": 80.0,
      "myScoreSchema": [
        {
          "average": 80.0,
          "count": 8.0,
          "percent": 80.0,
          "name": "John"
        },
        {
          "average": 0.0,
          "count": 0.0,
          "percent": 0.0,
          "name": "Cena"
        },
        {
          "average": 0.0,
          "count": 0.0,
          "percent": 0.0,
          "name": "Sunny"
        },
        {
          "average": 0.0,
          "count": 0.0,
          "percent": 0.0,
          "name": "Michael"
        }
      ]
    }

but the log as of the line Logger.info("body is #{inspect(body)}") from above code dedups the data and returns below data instead

{
      "avgPass": 85.55,
      "avgFail": 14.45,
      "totalStudents": 80.0,
      "myScoreSchema": [
        {
          "average": 80.0,
          "count": 8.0,
          "percent": 80.0,
          "name": "John"
        },
        {
          "average": 0.0,
          "count": 0.0,
          "percent": 0.0,
          "name": "Cena Sunny Michael"
        }
      ]
    }

Even though this is a smart feature but I don’t want this dedup feature. How to avoid the dedup.

HTTPoison isn’t going to do any deduplification for you. Try doing curl http://localhost:8080/getScoreData and see what data you get. HTTPoison just returns what is sent, it doesn’t change it.

I am not sure why but with the exact data provided above, I am seeing the deduplication done by HTTPoison. You can see the logging line Logger.info("body is #{inspect(body)}") right after call to HTTPoison.get.

I have never seen such a deduplication taking place, as it shouldn’t happen.

Please provide an example we can actually use to reproduce the problem. This includes having a public HTTP endpoint to pull the data from.

Well, you show the result from HTTPoison yes. I’m not sure that “deduplification” is even the right way to think about it though, it’s just flat out a different result. HTTPoison doesn’t know that this is JSON, so it isn’t even possible for it to construct this result on its own.

You haven’t actually provided any screen shots from the browser or the full output from using curl, so as of right now all I know is that there is a difference between what is shown by HTTPoison and what is shown in the browser. It looks like you’re hitting a local server, can you perhaps show us that code too?

I wouldn’t even call this “deduplication” - this is combining values, with a separator no less.

I don’t believe this line is related to your question, but it’s still odd - Jason doesn’t support doing this kind of decoding, that’s a feature of Poision.decode!/2.

1 Like