如何对Slack的网址发帖?

时间:2016-03-27 20:31:32

标签: json url post curl slack

我无法对Slack的网址进行简单的POST。

我需要这样的POST:

{ "text": "" }

https://something.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX

类似的网址

2 个答案:

答案 0 :(得分:0)

首先尝试使用curl命令

curl -X POST -H 'Content-type: application/json' --data '{"text":"This is a line of text.\nAnd this is another one."}' https://something.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX

如果它的工作,那么它的一块蛋糕

OkHttpClient client = new OkHttpClient();

MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{\"text\":\"This is a line of text.\\nAnd this is another one.\"}");
Request request = new Request.Builder()
  .url("https://something.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX")
  .post(body)
  .addHeader("content-type", "application/json")
  .addHeader("cache-control", "no-cache")
  .build();

Response response = client.newCall(request).execute();

答案 1 :(得分:0)

像这样的curl命令可以工作:

read -r -d '' PAYLOAD << EOM
{"attachments": [{
    "text": "Hello world!"
  }]
}
EOM
curl --data-urlencode "payload=$PAYLOAD" "https://something.slack.com/services/XXXXXXXX/XXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXX"