I'm using Poison to encode a map to JSON that will send it to the Slack API. This is what Poison gives me:
"{\"text\":\"changed readme fad996e98e04fd4a861840d92bdcbbcb1e1ec296\"}"
When I put that into JSON lint it says it is valid JSON, but Slack responds "invalid payload".
If I change the JSON to look like this
{"text":"changed readme fad996e98e04fd4a861840d92bdcbbcb1e1ec296"}
Then it works. Does anyone know where I'm going wrong with this? Do I need to do extra processing on the encoded JSON or is there some header I need to set?
Here is my controller
def create(conn, opts) do
message = Message.create_struct(opts)
response = Slack.Messages.send(message)
case response do
{:ok, data} ->
render conn, json: Poison.encode!(data)
{:error, reason} ->
render conn, json: reason
end
end
Here is part of the library for sending the messages
defmodule Slack.Messages do
def format_simple_message(map) do
text = map.description <> " " <> map.commits
message = %{text: text}
end
def post_to_slack(map) do
Slack.post(:empty, map)
end
def send(map) do
map
|> format_simple_message
|> post_to_slack
end
end
And my HTTPoison processing
defmodule Slack do
use HTTPoison.Base
@endpoint "http://url.com"
def process_url() do
@endpoint
end
def process_response_body(body) do
body
|> Poison.decode! # Turns JSON into map
end
def process_request_body(body) do
body
|> Poison.encode! # Turns map into JSON
end
end
The part that creates the JSON is in the last block.
json(conn, Poison.encode!(data))
instead ofjson(conn, data)
. – Breedlovechat.postMessage
API endpoint? The former acceptsapplication/json
(but you should make sure aContent-type: application/json
HTTP header is being set) while the latter does not support POSTing JSON-based messages this way. – Danikadanila