Missed Team ’24? Catch up on announcements here.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

prantish July 10, 2017

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

0 votes
Syahrul
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
July 14, 2017

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 

Sam Kassa December 18, 2017

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
TAGS
AUG Leaders

Atlassian Community Events