Sending Attachment to TFS work item through REST API (setRequestBodyFromAttachment)

jyeon110
Tera Expert

Hello experts,

 

I have a requirement to send INC information and attachment to TFS (similar to ADO) work item. Sending INC info works great, but I am having trough with sending the attachment files to the work item.

Current setup is that we have UI Action on INC to trigger Script Include which sends out REST MESSAGE to TFS.

One of the issues is that when the TFS ticket is created, attachment also appears in the ticket BUT there is no content. and REST Message doesn't send any data, file size is just 0k.

 

One of the function in the Script include:

function _uploadAttachementToTFS(fileSysId){
  var r = new sn_ws.RESTMessageV2("Example1", "Method1");
  r.setRequestHeader("Accept", "application/json");
  r.setRequestHeader("Content-Type", "application/octet-stream");
  r.setRequestBodyFromAttachment(fileSysId);
  var body = r.getRequestBody();
  var response = r.execute();
	
  var responseBody = JSON.parse(response.getBody());
	
    return responseBody.url; // Returns the URL of the uploaded attachment 
}

When I use "setRequestBodyFromAttachment" method, it doesn't set any request body, which just return null.

 

Does anybody know how to fix this issue?

 

Thank you in advance.

1 ACCEPTED SOLUTION

Sanjay191
Tera Sage

Hello @jyeon110 

Root Cause Analysis

  1. Attachment Retrieval Issue:

    • The script might not be retrieving the attachment content correctly from the ServiceNow record.
    • ServiceNow stores attachments as binary data in the sys_attachment and sys_attachment_doc tables. If the script does not fetch this data correctly, the REST message payload will lack the actual content.
  2. REST API Payload Construction:

    • When sending the attachment via a REST message, the payload must properly encode the file content (e.g., Base64 encoding). If this step is skipped or incorrectly implemented, the file will appear in TFS with no content.
  3. Content-Type Header:

    • The Content-Type header in the REST message might not match the expected format for TFS. For attachments, it is often multipart/form-data or application/octet-stream, depending on the API specification.
  4. TFS API Handling:

    • Ensure that the TFS API endpoint you are using to add attachments is correctly configured to receive and process file content.

Resolution Steps

1. Retrieve the Attachment Content Correctly

Use the sys_attachment API in ServiceNow to fetch the attachment content. Here’s an example:

 
var attachmentContent = getAttachmentContent(attachmentSysId); function getAttachmentContent(attachmentSysId) { var attachment = new GlideSysAttachment(); var content = attachment.getBytes(attachmentSysId); if (!content) { gs.error("Failed to retrieve attachment content."); return null; } return content; }

This method fetches the binary content of the file.


2. Encode the File Content

REST APIs often require file content to be Base64-encoded before sending. Encode the file content as follows:

 
var encodedContent = GlideStringUtil.base64Encode(attachmentContent);

3. Construct the REST Message Payload

Build a payload that includes the attachment in the format expected by TFS. For example:

 
var requestBody = { "fileName": "attachment_name.txt", // Replace with your attachment name "fileContent": encodedContent // Base64-encoded content };

4. Configure the REST Message

Ensure the Content-Type header is set correctly. For example:

 
var r = new sn_ws.RESTMessageV2('TFS API', 'POST'); r.setRequestHeader("Content-Type", "application/json"); r.setRequestBody(JSON.stringify(requestBody));

5. Debug and Test

  • Log and test each step to ensure that the attachment content is correctly fetched, encoded, and sent.
  • Use ServiceNow’s REST API Explorer or tools like Postman to validate the request payload against the TFS API.

Key Considerations

  • Check the TFS API documentation to confirm the required format and headers for attachments.
  • Ensure the attachment content is not excessively large. If it exceeds the allowed size for TFS, consider implementing a check or splitting the data.



If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You

View solution in original post

1 REPLY 1

Sanjay191
Tera Sage

Hello @jyeon110 

Root Cause Analysis

  1. Attachment Retrieval Issue:

    • The script might not be retrieving the attachment content correctly from the ServiceNow record.
    • ServiceNow stores attachments as binary data in the sys_attachment and sys_attachment_doc tables. If the script does not fetch this data correctly, the REST message payload will lack the actual content.
  2. REST API Payload Construction:

    • When sending the attachment via a REST message, the payload must properly encode the file content (e.g., Base64 encoding). If this step is skipped or incorrectly implemented, the file will appear in TFS with no content.
  3. Content-Type Header:

    • The Content-Type header in the REST message might not match the expected format for TFS. For attachments, it is often multipart/form-data or application/octet-stream, depending on the API specification.
  4. TFS API Handling:

    • Ensure that the TFS API endpoint you are using to add attachments is correctly configured to receive and process file content.

Resolution Steps

1. Retrieve the Attachment Content Correctly

Use the sys_attachment API in ServiceNow to fetch the attachment content. Here’s an example:

 
var attachmentContent = getAttachmentContent(attachmentSysId); function getAttachmentContent(attachmentSysId) { var attachment = new GlideSysAttachment(); var content = attachment.getBytes(attachmentSysId); if (!content) { gs.error("Failed to retrieve attachment content."); return null; } return content; }

This method fetches the binary content of the file.


2. Encode the File Content

REST APIs often require file content to be Base64-encoded before sending. Encode the file content as follows:

 
var encodedContent = GlideStringUtil.base64Encode(attachmentContent);

3. Construct the REST Message Payload

Build a payload that includes the attachment in the format expected by TFS. For example:

 
var requestBody = { "fileName": "attachment_name.txt", // Replace with your attachment name "fileContent": encodedContent // Base64-encoded content };

4. Configure the REST Message

Ensure the Content-Type header is set correctly. For example:

 
var r = new sn_ws.RESTMessageV2('TFS API', 'POST'); r.setRequestHeader("Content-Type", "application/json"); r.setRequestBody(JSON.stringify(requestBody));

5. Debug and Test

  • Log and test each step to ensure that the attachment content is correctly fetched, encoded, and sent.
  • Use ServiceNow’s REST API Explorer or tools like Postman to validate the request payload against the TFS API.

Key Considerations

  • Check the TFS API documentation to confirm the required format and headers for attachments.
  • Ensure the attachment content is not excessively large. If it exceeds the allowed size for TFS, consider implementing a check or splitting the data.



If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Thank You