How to upload attachments using Web Services API?
Later in the article see links to java code samples that attach existing zip and image files to Rally 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 Artifact 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="}}
Here is the result's JSON:
Here is the ref of the newly created AttachmentContent object:
https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/<ObjectID>
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 AttachmentContent 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/<Workspace OID>
There are different ways to find 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 attachmentcontent 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 <Story ObjectID>.
ObjectID of a Rally 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/<ObjectID>",
"Artifact":"https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/<Story ObjectID>",
"ContentType":"application/octet-stream",
"Name":"someAttachement.txt",
"User":"https://rally1.rallydev.com/slm/webservice/v2.0/user/<User ObjectID>"
}
}
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/<TC_Result_ObjectID>"
Here is the result's JSON:
Verify that the attachment was uploaded. For further verification, download the attachment and check its content:
*****To upload multiple attachments to a User Story the following can be used:
Endpoint:
https://rally1.rallydev.com/slm/webservice/v2.0/batch
Payload is below:
{
"Batch": [
{
"Entry": {
"Path": "/attachment/create",
"Method": "post",
"Body": {
"Attachment": {
"Content": "https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/<ObjectID>",
"Artifact": "https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/<ObjectID>",
"ContentType": "application/octet-stream",
"Name": "BatchAttachement1.txt",
"User": "https://rally1.rallydev.com/slm/webservice/v2.0/user/<ObjectID>"
}
}
}
},
{
"Entry": {
"Path": "/attachment/create",
"Method": "post",
"Body": {
"Attachment": {
"Content": "https://rally1.rallydev.com/slm/webservice/v2.0/attachmentcontent/<ObjectID>",
"Artifact": "https://rally1.rallydev.com/slm/webservice/v2.0/hierarchicalrequirement/<ObjectID>",
"ContentType": "application/octet-stream",
"Name": "BatchAttachement2.txt",
"User": "https://rally1.rallydev.com/slm/webservice/v2.0/user/<ObjectID>"
}
}
}
}
]
}