tpetes :
I'm trying to send a message using Slack incoming webhooks. I have the following code. It runs, but when I check my slack, there is no message. Can anyone see what I may have done wrong.
public class SlackTest {
static String web_hook_url = "https://hooks.slack.com/services/***********/******************";
public static void main(String[] args) {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(web_hook_url);
try {
String json = "{\"name\": John}";
System.out.println(json);
StringEntity entity = new StringEntity(json);
httpPost.setEntity(entity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
client.execute(httpPost);
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
tpetes :
I first used Postman to test sending the message. Then used Postman to generate the corresponding Java code. Then adjusted the code a bit to arrive at the following . . .
OkHttpClient client2 = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, "{ \"text\" : \"more text"\" }");
Request request2 = new Request.Builder()
.url("https://hooks.slack.com/services/********/*********/***************")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("Accept", "*/*")
.addHeader("Cache-Control", "no-cache")
.addHeader("Host", "hooks.slack.com")
.addHeader("accept-encoding", "gzip, deflate")
.addHeader("Connection", "keep-alive")
.addHeader("cache-control", "no-cache")
.build();
Response response2 = client2.newCall(request2).execute();