Sending JSON to Slack in a HTTP POST request

后端 未结 11 769
隐瞒了意图╮
隐瞒了意图╮ 2020-12-24 05:21

I\'m trying to send a message using Slack\'s chat.postMessage API call. I have no problems encoding my test messages within HTTP GET, but I\'m trying to achieve the same res

11条回答
  •  甜味超标
    2020-12-24 05:57

    Okay, after re-reading the documentation I found it:

    JSON-encoded bodies

    For these write methods, you may alternatively send your HTTP POST data as Content-type: application/json.

    There are some ground rules:

    • You must explicitly set the Content-type HTTP header to application/json. We won't interpret your POST body as such without it.
    • You must transmit your token as a bearer token in the Authorization HTTP header.
    • You cannot send your token as part of the query string or as an attribute in your posted JSON.
    • Do not mix arguments between query string, URL-encoded POST body, and JSON attributes. Choose one approach per request.
    • Providing an explicitly null value for an attribute will result in whichever default behavior is assigned to it.

    And the curl example. Notice the Authorization header.

    curl -X POST \
         -H 'Authorization: Bearer xoxb-1234-56789abcdefghijklmnop' \
         -H 'Content-type: application/json; charset=utf-8' \
        --data '{"channel":"C061EG9SL","text":""..."}' \
    https://slack.com/api/chat.postMessage
    

提交回复
热议问题