Oscar_dev

Oscar_dev

Sending HTTPoison from phenix app to a device on the same network

Hello,

I am trying to send a request to a server on the same network.

  def unload_model(conn, %{"id" => id}) do
    case Frontend.Repo.get(LoadedModel, id) do
      nil ->
        {:error, "LoadedModel with id #{id} not found"}

      loaded_model ->
        # Construct the URL and payload for the DELETE request
        url = "http://10.0.20.78:8000/admin/unoad_model"
        payload = %{model_id: id}

        headers = [
          {"Content-Type", "application/json"},
          {'api_key', conn.assigns.current_user.api_key}
        ]

        # Send the DELETE request to the server
        case send_delete_request(url, payload, headers) do
          {:ok, _response} ->
            {:ok, "Model unloaded successfully"}

          {:error, reason} ->
            {:error, "Failed to unload model: #{reason}"}
        end
    end
  end
  defp send_delete_request(url, payload, headers) do
    case HTTPoison.delete(url, body: Jason.encode!(payload), headers: headers) do
      {:ok, response} ->
        {:ok, response}
      {:error, %HTTPoison.Error{reason: reason}} ->
        {:error, "HTTPoison error: #{inspect(reason)}"}
    end
  end

Earlier i got errors regarding SSL certificate:

{:tls_alert, {:unknown_ca, 'TLS client: In state wait_cert_cr at ssl_handshake.erl:2111 generated CLIENT ALERT: Fatal - Unknown CA\n'}}

After that, i started getting a new error:

errors were found at the given arguments:
  * 1st argument: not an iodata term

I think this is whats causing the error
:erlang.list_to_binary([{"Content-Type", "application/json"}, {"api_key", "...."}])

i assume the error is related to the body of the request being unrealizable or something like that, i’ve tried many things, but i don’t know what else to do.

Thanks

Marked As Solved

_jonas

_jonas

I think this

Should be just

case HTTPoison.post!(url, Jason.encode(payload), headers) do

Take this example from the docs for reference:

I’m also just learning Elixir but I think body: Jason.encode(payload), headers: headers) is syntactic sugar for [body: Jason.encode(payload), headers: headers)] meaning both arguments become a single keyword list that is used as the second argument of the function call.
Someone correct me if I’m wrong here.

Also Liked

dwark

dwark

From: IO — Elixir v1.20.2

IO data is a data type that can be used as a more efficient alternative to binaries in certain situations.

A term of type IO data is a binary or a list containing bytes (integers within the 0..255 range) or nested IO data. The type is recursive. Let’s see an example of one of the possible IO data representing the binary "hello":

[?h, "el", ["l", [?o]]]

The built-in iodata/0 type is defined in terms of iolist/0. An IO list is the same as IO data but it doesn’t allow for a binary at the top level (but binaries are still allowed in the list itself).

In your case, the list you provide to :erlang.list_to_binary/1 contains tuples, which is not allowed in an iolist.

dwark

dwark

I am not familiar with HTTPoison, but the docs for HTTPoison.get/3 seem to suggest that headers can be given as the second argument:

get(url, headers \\ [], options \\ [])

See the type definition for headers.

headers() ::
  [{atom(), binary()}]
  | [{binary(), binary()}]
  | %{required(binary()) => binary()}
  | any()

So it looks like you do not need to convert your headers (it’s already a list of {binary, binary}-tuples. Not sure where the error is coming from though. Perhaps some part or element of your headers-list is not conform above spec?

Oscar_dev

Oscar_dev

You are absolutely right thank you so much, if i have follow up errors do i continue on this thread, i don’t know the etiquette here.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New

We're in Beta

About us Mission Statement