Rally1

Expand all | Collapse all

How to add an Attachment to a User Story using Rally Rest API

Jump to Best Answer
  • 1.  How to add an Attachment to a User Story using Rally Rest API

    Posted 10-18-2019 12:39 PM
     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
    ------------------------------


  • 2.  RE: How to add an Attachment to a User Story using Rally Rest API

    Posted 10-18-2019 03:35 PM
    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


  • 3.  RE: How to add an Attachment to a User Story using Rally Rest API
    Best Answer

    Posted 10-22-2019 08:46 PM
    Hi Regi,

    I received an email from you, but for some reason I can't reply. I'm getting an 'undelivered' message. Below is your email that I had received.
    I believe you are using the ObjectUUID instead of the ObjectID. So, when you look at the error message that shows: "7e5e07ca-bb9f-4725-82b6-5528266f3f34" , this value is of an ObjectUUID. However, you need to use the ObjectID instead.

    Can you try that and let us know if it worked.

    Thanks,
    Sagi

    Your email
    Hi Sagi

    Thanks for your detailed explanation. 

    While following your steps,  In step 2 , I am getting the following error.

    I got the OID from the first step - and I am trying to reference that in Step2.

    USER_DEBUG [92]|DEBUG|{"CreateResult": {"_rallyAPIMajor": "2", "_rallyAPIMinor": "0", "Errors": ["Could not read: Could not read referenced object /attachmentcontent/7e5e07ca-bb9f-4725-82b6-5528266f3f34"], "Warnings": []}}

    The code I tried is

    description = 'This is my story'; description = 'This is my story';     

    //String UsContent = '"_ref":"/attachmentcontent/'+ Jstring + '"' ;      
    System.debug('UsContent' + UsContent);       
    endpoint = 'https://rally1.rallydev.com/slm/webservice/v2.0/attachment/create';   
     HttpRequest request1 = new HttpRequest();     
      request.setEndPoint(endpoint);     
      request.setBody('{"Attachment":{"Description": "{description}","ContentType":"text/plain","User":"/user/195975401488","Content":{"_ref":"/attachmentcontent/7e5e07ca-bb9f-4725-82b6-5528266f3f34"}, "Name":"TestFile.docx"}}'); 
     request.setMethod('PUT');   
      HttpResponse response1= httpProtocol.send(request);     
      System.debug(response1.getStatus());       
      System.debug(response1.getBody());


  • 4.  RE: How to add an Attachment to a User Story using Rally Rest API

    Posted 10-23-2019 09:28 PM
    Thank you Regi.
    I'm glad it worked out for you. :)

    Your email
    Thanks Sagi, It worked. 

    OID did the magic.

    Thanks for your support.


    ------------------------------
    RV