YouTrack InCloud 2019.2 Help

Attach Files to an Issue

Use Case:

Use the REST API to attach several files to an issue.

Summary

To attach a file to an issue:

  1. Get a list of issues to get the entity ID of the target issue.

  2. Attach files to the issue by sending a POST request to the following endpoint:

    /api/issues/{issueID}/attachments

Step-by-Step

  1. Get the list of issues in a target project.

    curl -X GET 'https://example.myjetbrains.com/youtrack/api/issues?query=in:NP&$top=3&fields=id,idReadable,summary' \ -H 'Accept: application/json' \ -H 'Authorization: Bearer perm:cm9vdA==.MjZGZWI=.WB02vjX0cM2ltLTJXUE3VOWHpJYYNx' \ -H 'Content-Type: application/json'

    The query request parameter lets us filter the list of issues by a search query. In this sample, we use the query to get only the issues that belong to the target project (NP).

    For this particular example, to shorten the list of returned issues, we use $top=3 parameter to get only first three found issues.

    In the fields request parameter, we instruct the server to return the following attributes for each issue found:

    • id and idReadable - both database entity ID and an ID of the issue in the project, respectively.

    • summary - issue's summary may help to identify the target issue.

    In the response to this request, the server returns the following data:

    [ { "summary": "Investigation task", "idReadable": "NP-56", "id": "99-356", "$type": "Issue" }, { "summary": "Marketing activities", "idReadable": "NP-39", "id": "99-303", "$type": "Issue" }, { "summary": "TODO for release", "idReadable": "NP-77", "id": "99-500", "$type": "Issue" } ]
  2. Locate the ID of the target issue. In this case, let's take the issue NP-77 with the database ID 99-500.

  3. To attach files to an issue, we need to send a POST request with the attachments to this endpoint: /api/issues/{issueID}/attachments. For this use case, we use the following cURL command:

    curl -v -i -F upload=@/Users/jetbrains/Downloads/youtrack.txt \ -F upload=@/Users/jetbrains/Downloads/youtrack_new.jpeg \ -H 'Accept: application/json' \ -H 'Authorization: Bearer perm:cm9vdA==.MjZGZWI=.WB02vjX0cM2ltLTJXUE3VOWHpJYYNx' \ -H 'Content-Type: multipart/form-data' \ -X POST 'https://example.myjetbrains.com/youtrack/api/issues/99-500/attachments?fields=id,name'

    Here's the key parameters of the request that we used:

    • -F option lets us upload the required files to the server.

      -F upload=@/Users/jetbrains/Downloads/youtrack.txt - the path to the file that should be attached to the target issue. You can attach several files in one request:

      -F upload=@/Users/jetbrains/Downloads/youtrack.txt \ -F upload=@/Users/jetbrains/Downloads/youtrack_new.jpeg

      The @ prefix indicates that the following content part is a file.

    • Content-Type: multipart/form-data: use this header to attach files.

    In the response, the server returns the name and database entity ID of the attached files:

    [ {"name":"youtrack.txt","id":"134-31","$type":"IssueAttachment"}, {"name":"youtrack_new.jpeg","id":"134-32","$type":"IssueAttachment"} ]

That’s it: we’ve attached all our files to the issue.

Last modified: 17 October 2019