如何使用 Postman post 带有图像的消息
How to post a message with an image using Postman
我正在尝试从 POSTMAN 发送图像。我可以发送消息,但无法发布图片。
https://slack.com/api/chat.postMessage
已使用 POST 类型 headers 我正在传递令牌、通道并且我有一个图像 URL 但不确定如何发送它。谁能帮我解决这个问题。
关于如何将消息发送到 API 端点,有两种替代方法 chat.postMessage
:
Body 作为 form-urlencoded
以下是如何在作为 x-www-form-urlencoded
:
发送的消息中包含图像
图像必须作为附件的一部分发送,方法是将 属性 设置为 image_url
。
附件是通过 API 调用中的 attachments
键设置的,这需要您将附件定义为 JSON 中的附件数组 object。
除了 image_url 之外,您的附件还需要包含后备 属性,用于在无法显示图像时向用户显示文本。
您的附件 object 在 JSON 中看起来像这样:
{
"fallback": "this did not work",
"image_url": "https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"
}
现在你必须把它放入一个数组中(通过在它周围添加 []),这就是你的 attachments
键的值:
[{"fallback":"did not work","image_url":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"}]
此外,您需要在消息中添加键 token
、channel
、文本 key
。瞧。
Body 作为 JSON
另一种可能更简单的方法是将您的数据作为 JSON 而不是 x-www-form-urlencoded
发送到 API。
这需要您在 Auth header 中发送令牌并切换到 body 的 JSON。
要在 Postman 中执行此操作,请将您的令牌作为 "Bearer Token" 放在 "Authorization" 下。
在 "Body" 切换到 "raw" 和 select "JSON".
然后你可以定义整个消息如下:
{
"channel": "test",
"text": "Hi there!",
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"
}
]
}
当然你可以在你的代码中做同样的事情。由于您在 JavaScript 工作,因此使用 JSON 将是自然的方法。
请注意,并非所有 API 方法都支持在 JSON 中发送 body。查看 this documentation 了解更多信息。
我正在尝试从 POSTMAN 发送图像。我可以发送消息,但无法发布图片。
https://slack.com/api/chat.postMessage
已使用 POST 类型 headers 我正在传递令牌、通道并且我有一个图像 URL 但不确定如何发送它。谁能帮我解决这个问题。
关于如何将消息发送到 API 端点,有两种替代方法 chat.postMessage
:
Body 作为 form-urlencoded
以下是如何在作为 x-www-form-urlencoded
:
图像必须作为附件的一部分发送,方法是将 属性 设置为 image_url
。
附件是通过 API 调用中的 attachments
键设置的,这需要您将附件定义为 JSON 中的附件数组 object。
除了 image_url 之外,您的附件还需要包含后备 属性,用于在无法显示图像时向用户显示文本。
您的附件 object 在 JSON 中看起来像这样:
{
"fallback": "this did not work",
"image_url": "https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"
}
现在你必须把它放入一个数组中(通过在它周围添加 []),这就是你的 attachments
键的值:
[{"fallback":"did not work","image_url":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"}]
此外,您需要在消息中添加键 token
、channel
、文本 key
。瞧。
Body 作为 JSON
另一种可能更简单的方法是将您的数据作为 JSON 而不是 x-www-form-urlencoded
发送到 API。
这需要您在 Auth header 中发送令牌并切换到 body 的 JSON。
要在 Postman 中执行此操作,请将您的令牌作为 "Bearer Token" 放在 "Authorization" 下。 在 "Body" 切换到 "raw" 和 select "JSON".
然后你可以定义整个消息如下:
{
"channel": "test",
"text": "Hi there!",
"attachments":
[
{
"fallback": "this did not work",
"image_url": "https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"
}
]
}
当然你可以在你的代码中做同样的事情。由于您在 JavaScript 工作,因此使用 JSON 将是自然的方法。
请注意,并非所有 API 方法都支持在 JSON 中发送 body。查看 this documentation 了解更多信息。