Formatting Issue for API call

I think I have a simple formatting issue, but after hours of moving stuff around, I just can’t get the syntax right. I bet someone out there will see the error verry quickly. I’m working in “developer tools” | “actions” to test an api to slack. Connections aren’t a problem and the api, when called from curl works fine. Here’s the way I have formatted a test call to the API. Payload syntax appears to be the issue. The X’s below just hide my real channel ID.

action: rest_command.slack_api
data_template:
  api: 'chat.postMessage'
  payload: >
    {
      "channel": "XXXXXXXXXXX",
      "text": "This is from actions",
      "attachments": '[]'
    }

When I run the action I get:

content:
  ok: false
  error: invalid_arguments
  response_metadata:
    messages:
      - "[ERROR] missing required field: channel"
status: 200

Try this version.

action: rest_command.slack_api
data_template:
  api: 'chat.postMessage'
  payload: >
    {{ {
        "channel": "XXXXXXXXXXX",
        "text": "This is from actions",
        "attachments": "[]"
       } | tojson }}

Note the consistent use of double-quotes delimiting the data fields and its conversion to JSON.

I don’t recall if Developer Tools → Actions supports the testing of actions containing templates. If it doesn’t then you’ll need to test it within whatever automation/script you intend to use for this action.


NOTE

I searched the forum for rest_command.slack_api and found only 6 posts. I am unfamiliar with the structure of Slack’s API and couldn’t find an example similar to yours. Most didn’t use tojson. If what I offered fails to work then I suggest you review those 6 posts for clues. Good luck!

Thank you for your suggestion. The resolution of “payload” in the browser console from this suggestion was:

  {{ {
    "channel": "#XXXXXXXX",
    "text": "This is from actions",
    "attachments": "[]"
   } | tojson }}

From my working curl attempts it should look like:

{"channel":"ChannelIDHere","text":"I something something."}

I’ve tried this (amongst a plethora of permutations) and the results look close:

action: rest_command.slack_api
data:
  api: 'chat.postMessage'
  payload: '{"channel":"XXXXX","text":"Channel is full text on this one"}'
  
 RESULT from browser console: {"channel":"#XXXXXX","text":"Channel is full text on this one"}

The result to this looks good, but still fails with the same error as in my first message. Within the browser console itself, the actual result has a lot of escaping going on for the special characters. I am suspecting that this is the issue. Not sure how to suppress that behavior. There is a backslash (\) character in front of all the double quotes (") in the browser console variable.

Which means it didn’t process the template but sent it as a literal string. If you tested it via Developer Tools → Actions then it means it doesn’t support the use of templates.

I suggest you test it as an action within a script.

Interesting. I’ll try that. I was attempting to test each component on its own. API set up via curl. Then, Rest API by itself. Then add a script on top. Etc. I guess this was an unworkable idea. Appreciate the observation.