Pretty-Print JSON message
What happened
I have a slack bot that outputs/sends a JSON message back to the user. JSON message is not pretty-printed to the user. JSON message is printed like a text message.
Using the PostMessage method for sending the message.
I tried various MsgOption like Blocks (section, context, input, fileand others), Text, Attachments, but still not able to pretty-print the JSON message.
Looking at the Slack API documentation, I see https://api.slack.com/methods/chat.postMessage
When POSTing with application/x-www-form-urlencoded data, the optional attachments argument should contain a JSON-encoded array of attachments. Make it easy on yourself and send your entire messages as application/json instead.
But, I see in the Slack Go Content-Type in the header is being set to application/json only for "chat.responseURL"
https://github.com/slack-go/slack/blob/93fe17cfad827ebb34316c683f29377c9be910e3/misc.go#L121
For all others, Content-Type in the header is being set to application/x-www-form-urlencoded
https://github.com/slack-go/slack/blob/93fe17cfad827ebb34316c683f29377c9be910e3/misc.go#L107
Is there an easy way to send over JSON messages with any other APIs? or set the header Content-Type to application/json so as to pretty-print the JSON message.
Any help or suggestions will be really helpful - @kanata2 @nlopes
Expected behavior
JSON message being printed as of now: {"uuid":"123","namespace":"abc","pool":"abcd"}
expected pretty-print JSON format:
{
"uuid":"123",
"namespace":"abc",
"pool":"abcd"
}
Steps to reproduce
Versions
- Go:
go version go1.14.3 darwin/amd64 - slack-go/slack:
https://github.com/slack-go/slack v0.8.2
Thanks for reporting! I think whether we should use pretty print or not is one thing, whether we should use application/json or not is another. What problem do you resolve? (do you want more debuggability in development?)
Thanks for getting back.
I think for pretty printing the JSON we do need to provide an option for client users to set the header with application/json so that the text message can be interpreted as JSON (Pretty print in Slack). Please do correct me if I am wrong.
I am looking for an easy way to pretty-print the JSON message for the users of our bot to read JSON more easily. Having JSON printed as a text doesn't help in reading the JSON message.
Can we pretty-print the JSON by message formatting? like Blocks or Attachments or others? Or do we have any APIs or settings in here to do this already?
I'm sorry. I'm not sure about your use case. Which JSON do you want to have prettty-printed output?(Is it the body of an HTTP request sent to Slack?)
sorry for the confusion. Yes, it's the body (actual message displayed to the user on slack) of the HTTP request I want to pretty print.
Yes, it's the body (actual message displayed to the user on slack) of the HTTP request I want to pretty print.
I understood! Thanks.
@kanata2 if you can let me know how I can handle this, I can make the changes and raise a PR. Please let me know.
How does it work with ruby hashes? I rather have JSON not formatted and my ruby code stays as it was.