Upload attachments using Web Services API

Document ID : KB000047770
Last Modified Date : 14/02/2018
Show Technical Document Details

Summary

Creation of attachment via WS API from scratch consists of two steps: creation of the attachmentcontent object and creation of the actual attachment on a CA Agile Central artifact.

Issue

How to upload attachments using Web Services API?

Resolution

Creation of attachment via WS API from scratch consists of two steps: creation of the attachmentcontent object and creation of the actual attachment on a CA Agile Central artifact.
Later in the article see links to java code samples that attach existing zip and image files to CA Agile Central artifacts.

The example below applies to objects in WS API that inherit from Artifact object, e.g. user story(HierarchicalReqirement in WS API), defect, test case, as well as to TestCaseResult object , which is outside of Artiact object's hierarchy in WS API. (TestCaseResult directly inherits from WorkspaceDomainObject. See Object Model for more details.)
The only difference is that attachment payload for a test case result has to use TestCaseResult, and not Artifact object.

A browser REST client is used to post both objects, and a valid API Key is used for authentication, set in zsessionid header.

Before we proceed with the examples we need to encode? attachment content. Using this encoder/decoder,? we encoded "hello world" to UTF-8
aGVsbG8gd29ybGQ=
This code will be used in a payload when creating attachmentcontent object.

1. Create attachmentconent:

Endpoint:

https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/create

Payload:

{"AttachmentContent":{"Content":"aGVsbG8gd29ybGQ="}}


User-added image

Here is the result's JSON:

User-added image
Here is the ref of the newly created attachmentcontent object:
https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/31001285770

NOTE: As with any WS API request, a request to create attachmentcontent may be implicitly scoped to a workspace different from the one for which it is intended. In case the creation of the attachmentconent fails with "Not authorized to create Attachmentcontent" error try setting workspace on the request explicitly as shown in this example:

https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/create?workspace=https://rally1.rallydev.com/slm/webservice/v2.0/workspace/12345

where workspace ObjectID is 12345. There are different ways ot find out ObjectID of a workspace. One of them is to switch to that workspace in the UI and follow suggestions from this article: How to find ObjectIDs of current workspace and project without admin rights


If the attachmentconent has been successfully created will use it in the payload of the attachment object created in the next step.

2. Create attachment:

Identify an artifact on which you want to create an attachment.
In this example it is a user story with ObjectID of 29750715443.
ObjectID of a CA Agile Central artifact can be accessed from the last part of the URL of its details page.

Endpoint:

https://rally1.rallydev.com/slm/webservice/v2.0/attachment/create

Payload:

{
"Attachment":
{"Content":"https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/31001285770",
"Artifact":"https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/29750715443",
"ContentType":"application/octet-stream",
"Name":"someAttachement.txt",
"User":"https://rally1.rallydev.com/slm/webservice/v2.0/user/721165372"
}
}


NOTE: when creating an attachment on a test case result modify payload syntax to use TestCaseResult object instead of Artifact e.g.:

"TestCaseResult":"https://rally1.rallydev.com/slm/webservice/v2.0/testcaseresult/29750715443"


User-added image
Here is the result's JSON:

User-added image
Verify that the attachment was uploaded. For further verification, download the attachment and check its content:

User-added image


For real-life examples see java code samples here and here.
?