Hi Regi.
Yes, it's possible to create a user story with an attachment using Rally's API. However, it can't all be done in a single http call.
In your code there are a few issues:
- On the HierarchicalRequirement you're referencing a field named: "Attachment". The actual field's name is: "Attachments" (plural).
- You're trying to create the attachment together with creating the user story.
The reason you can't create the attachment with the user story is because Attachment is a separate endpoint, which also happens to reference an AttachmentContent object as well (that holds the actual contents). The process shall be: (1) create the AttachmentContent (2) create the Attachment and refer the newly created content (3) create the user story and refer the newly created attachment.
Here is a working example:
Step 1: Creating an AttachmentContent:
{"AttachmentContent":{"Content":"HERE YOU SHALL FILL IN YOUR BASE-64-BINARY FILE CONTENT"}}
You will receive an ObjectID for the newly created AttachmentContent, we're going to use it in step 2.
Step 2: Creating an Attachment:
{"Attachment":
{
"Description":"User Story with Attachment",
"User":"/user/<Fill in your user OID>",
"ContentType":"text/plain",
"Content": {
"_ref":"/attachmentcontent/<Fill in your AttachmentContent OID received in step1>"
},
"Artifact":"/artifact/<Fill in your artifact OID>",
"Name":"TestFile.docx"
}
}
You shall again receive an ObjectID of the newly created Attachment object. You will use it in step 3.
Step 3: Create a user story and include the attachment:
{
"hierarchicalrequirement":
{
"Name": "My User Story",
"Description: "Test User Story with Attachment",
"Project":"/project/<Fill in your project OID>",
"Attachments": [
{ "_ref": "/attachment/<Fill in the Attachment OID from step 2>" }
]
}
}
I hope this helps. Let us know if questions.
Sagi
Original Message:
Sent: 10-18-2019 12:38 PM
From: Regi Varghese
Subject: How to add an Attachment to a User Story using Rally Rest API
is it possible to send an attachment (.doc or a .xls) file along with other details while creating an user story using rest API?
I'm using the below JSON script and I'm getting a error "cannot find referenced object
'{"HierarchicalRequirement": { "Name": "Test SRM Connector",
"Description": "Please ignore this user story...",
"_refObjectName": "Regi Varghese",
"c_WorkType" : "Maintenance",
{"Attachment":{"Content":"aGVsbG8gd29ybGQ=","ContentType":"octet-stream","Name":"a.txt","Size":"20"}} } }');
request.setMethod('PUT');
Getting the below error:
USER_DEBUG [63]|DEBUG|{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Cannot parse input stream due to I/O error as JSON document: Parse error: expected '\"' but saw '{' [ chars read = \u003E\u003E\u003E{\"HierarchicalRequirement\": { \"Name\": \"Test SRM Connector\",\"Description\": \"Please ignore this user story...\",\"_refObjectName\": \"
Any help is appreciated.
Thanks
------------------------------
Regi
------------------------------