Rest API add attachments to comment

How add attachment to issues's comment wia Rest API?

I try this

encoderrrr =MultipartEncoder(fields={'file':('tets.jpg', open('./project/MAC/MAC-338/screenshot-1.png','rb'))})
auth_login,auth_password ='USER','PASSWORD'
headers = {
'Accept': 'application/json',
'Cache-Control': 'no-cache',
'Content-Type': encoderrrr.content_type}
r = requests.post('https://youtrack.URL.com/api/issues/{}/attachments/{}'.format('MAC-1','4-400'),
auth=HTTPBasicAuth(auth_login,auth_password),
data= encoderrrr,
headers=headers)

And i get error

{"error":"Not Found","error_description":"Entity with id 4-400 not found"}

How I can send attachment to comment?

 

0
1 comment
Official comment

Hello, thank you for your message.

First of all, we have a step-by-step tutorial on how to attach files to issues, please feel free to check it here: https://www.jetbrains.com/help/youtrack/standalone/api-usecase-attach-files.html The process of attaching files to comments is very similar.

As to your code snippets:

1) You need to use a permanent token instead of Basic Auth. Here you can learn more about token authorization for using our REST API: https://www.jetbrains.com/help/youtrack/standalone/authentication-with-permanent-token.html

2) The URL /issues/{issueID}/attachments/{attachID}  that you're using is for the files attached to an issue and not to a comment. If you need to attach files to comments, use this URL instead: /api/issues/{issueID}/comments/{commentID}/attachments

Note that here you don't need to paste the exact attachment ID at the end, but you do need to enter the comment ID. To get a comment ID, make a call to /api/issues/{issueID}/comments and identify the comment that you need to attach files to.

 

I hope this is helpful, please let me know if I can answer any further of your questions. 

Please sign in to leave a comment.