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
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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








