Tech Me More

To quench our thirst of sharing knowledge about our day to day experience & solution to techincal problems we face in our projects.

Advertise with us !
Send us an email at

Wednesday, July 17, 2019

How to send attachment in Slack channel using Restful Webservice

Slack : What comes to your mind when you hear word Slack ? It is probably one of best collaboration tool available in the market. Now a days Slack is also widely used across multiple organization of all level. [Big, mid or small] 

Now since everyone is active on Slack , it is of extreme importance to send messages to slack channels. This is when the need arise to send automated notifications. Slack provides a lot of rest api's to accomplish this task. 

In this post, We will cover how to send attachments in Slack channels using Slack endpoint with Java. 

  • You should have slack authentication token generated. 
  • Name of the slack channel where the message will be delivered. 

package com.dht.test;


import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClientBuilder;

public class SlackChannelAttachmentUtil{

 public static void main(String[] args) {
  try {
   String url = "";
   HttpClient httpclient = HttpClientBuilder.create().disableContentCompression().build();
   HttpPost httppost = new HttpPost(url);
   MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
   reqEntity.addBinaryBody("file", new File("C:\\"));
   reqEntity.addTextBody("channels", "attachment-testing");
   reqEntity.addTextBody("media", "file");
   reqEntity.addTextBody("initial_comment", "Hello This is from code");

   HttpResponse execute = httpclient.execute(httppost);
  } catch (Exception e) {



The above code will send file to attachment
-testing slack channel. You will also see Message "Hello This is from code" along with attachment.

No comments: