bourgy
September 25, 2018, 12:46am
1
I’m looking for a way to send send a custom string as a payload from an elixir controller.
The spec I’m working with expects something similar to this.
param1=value1\n
param2=value2\n
Name value pairs. One per line separated by a newline character.
How might one go about this?
Thank you for any help.
def some_action(conn, _params) do
...
conn
|> put_resp_content_type("text/whatever")
|> send_resp(200, encode(payload))
end
defp encode(%{} = payload) do
for {key, val} <- payload, into: "" do
"#{key}=#{val}\n"
end
end
8 Likes
bourgy
September 25, 2018, 2:24pm
3
Exactly what I was looking for. Thank you very much.
1 Like
chrismccord:
%{} = payload
what this mean in function encode, is equal defp encode(payload) when is_map(payload)
?
Yes but not only, it works for struct too. For example
%MyStruct{} = payload
This way You ensure pattern match will work only if payload is of a certain Struct, without the need to use guard.
2 Likes