How to share file with room in HipChat via Postman or Jersey client?

How to share file with room in HipChat via Postman or Jersey client? Please provide the solution for this. I tried with below link but unable to share the file, please can you help me on this. In below link if Hipchat provide any example its good to developers.

https://www.hipchat.com/docs/apiv2/method/share_file_with_room 

I included screen shots, which I tried from postman.

Note:  I used API access token with send message scope.

I am getting below error:

{
"error": {
"code": 400,
"message": "No file was found in the request. See https://www.hipchat.com/docs/apiv2/method/share_file_with_room for an example",
"type": "Bad Request"
}
}

10-Jul-17 7-43-33 PM.jpg10-Jul-17 7-44-01 PM.jpg

Thanks in advance 

1 answer

This widget could not be displayed.

Hi Prantish,

 

I'm not that good with the Postman, but if you are looking for a way to upload a file, I would suggest you try this python script:

#!/usr/bin/env python

from __future__ import print_function
import requests
import sys
import json
import os


def hipchat_file(token, room, filepath, message='', host='api.hipchat.com'):
""" Send file to a HipChat room via API version 2
Parameters
----------
token : str
HipChat API version 2 compatible token - must be token for active user
room: str
Name or API ID of the room to notify
filepath: str
Full path of file to be sent
message: str, optional
Message to send to room
host: str, optional
Host to connect to, defaults to api.hipchat.com
"""

if not os.path.isfile(filepath):
raise ValueError("File '{0}' does not exist".format(filepath))
if len(message) > 1000:
raise ValueError('Message too long')

url = "https://{0}/v2/room/{1}/share/file".format(host, room)
headers = {'Content-type': 'multipart/related; boundary=boundary123456'}
headers['Authorization'] = "Bearer " + token
msg = json.dumps({'message': message})

payload = """\
--boundary123456
Content-Type: application/json; charset=UTF-8
Content-Disposition: attachment; name="metadata"
{0}
--boundary123456
Content-Disposition: attachment; name="file"; filename="{1}"
{2}
--boundary123456--\
""".format(msg, os.path.basename(filepath), open(filepath, 'rb').read())

r = requests.post(url, headers=headers, data=payload)
r.raise_for_status()


my_token = 'token'
my_room = 'room id'
my_file = '/Users/syahrul/Desktop/file.csv'
my_message = 'Check out this cool file' # optional

try:
hipchat_file(my_token, my_room, my_file, my_message)
except Exception as e:
msg = "[ERROR] HipChat file failed: '{0}'".format(e)
print(msg, file=sys.stderr)
sys.exit(1) 

Just change the my_token to your APIv2 token and my_file to your file path location. Also, you can tweak the my_message to your message when sending the file.

 

Hope this helps.

Syahrul 

Hi 

Is there a way to do this in Java ?

I'm having an issue with it. See below. 

String file_path = "src/test/resources/sku/sku.zip";

File file = new File(file_path);

 // JSONObject message = new JSONObject();

// message.put("message", "sku_uploads");

 FileBody fileBody = new FileBody(file);

MultipartEntityBuilder builder = MultipartEntityBuilder.create().addPart("file", fileBody);

builder.addBinaryBody("file", file, ContentType.create("application/zip") , "sku.zip");

HttpEntity multiPartEntity = builder.build();

String boundary = "===" + System.currentTimeMillis() + "===";

// execute post and parse response data

httpPost = new HttpPost("https://${host}/v2/room/${room_name}/share/file");

httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Bearer ${token}");

//httpPost.setEntity(new StringEntity(message.toString(), "UTF-8"));

httpPost.setEntity(multiPartEntity);

 

httpPost.addHeader("Content-Type", "multipart/related; boundary="+ boundary);

// httpPost.addHeader("Content-Type", "application/json");

httpPost.addHeader("charset", "UTF-8");

handler = new BasicResponseHandler();

 

 

response = httpClient.execute(httpPost);

String body = EntityUtils.toString(response.getEntity(), "UTF-8");

System.out.println(body);

Assert.assertTrue(response.getStatusLine().toString().contains("200"));

 

 

 

 

Error message :

{

  "error": {

    "code": 400,

    "message": "No file was found in the request. See https://www.hipchat.com/docs/apiv2/method/share_file_with_room for an example",

    "type": "Bad Request"

  }

}

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 10, 2018 in Hipchat

What should I think about when migrating HipChat to Slack?

...from the beginning. We have built up a lot of content in HipChat, with it being a core tool in our distributed company model. While it is true that we didn’t need to move to Slack immediately, we felt it...

386 views 1 10
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you