How can I post /attach a file on Yammer using REST Api?

Parag Sanyashi2
Kilo Contributor

Below is the code snap... Please let me know is that possible, because the file is posted using Postman.

var target = new GlideRecord('sys_attachment');
target.addQuery('sys_id','432122c326a4f8b15f8121f3b718110c751');
target.addQuery('table_sys_id', 'ef43c6d40a41245700c77f9bf387afe3');
target.query();
gs.print(target);
while(target.next())
{
/*
var sa = new GlideSysAttachment();
var binData = sa.getBytes(target);
var base64Data = GlideStringUtil.base64Encode(binData);
*/

var clientToken = '0818014-pP7Fgt8w4g92uYFgeWABUGYDks';

var request = new sn_ws.RESTMessageV2();
        request.setHttpMethod('post');
        request.setRequestHeader("Authorization","Bearer"+clientToken);
//request.setRequestHeader("Accept","application/form-data");
  request.setRequestHeader('Content-Type','application/form-data');


        request.setEndpoint('https://www.yammer.com/api/v1/pending_attachments/');
        request.setRequestBodyFromAttachment('432122c326a4f8b15f8121f3b718110c751');
        var response = request.execute();
        gs.print(response);
        var httpResponseStatus = response.getStatusCode();
      
        gs.print("http response status_code: " + httpResponseStatus);    
4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Parag,

It should send the attachment data. Can you check with the 3rd party whether they are receiving any attachment data?

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

I have already referred the link but it was not working for my problem. Need some different help from that code snap-it.

 

Thanks for Reply 🙂

Do you any other alternative that will solve the issue. Because after executing the code it replies with server error 500.

 

Thanks

Hi Parag,

You need to check the API of yammer that should help you understand how are they expecting the attachment.

https://www.codeproject.com/Questions/408329/post-on-yammer-with-an-attachment

https://stackoverflow.com/questions/25172765/posting-yammer-message-with-attachment

Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.
Thanks
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader