Page 1 of 1

Add attachments to issue via API

Posted: 26 Aug 2020, 14:01
by falexandre90650
Hi all !

I try to add attachments to an issue via API as explained in that website : https://documenter.getpostman.com/view/ ... 259c5706ef

I am using a Python script which follows :

Code: Select all

url_issue = "{{url_Mantis}}/api/rest/issues/{{id_Mantis_issue}}/notes"
payload = "{\n  \"files\": [\n  \t{\n  \t\t\"name\": \"test.pdf\",\n  \t\t\"content\": \"C:/Users/admin/Desktop/Doc7.pdf\"\n  \t}  \t\n  ]\n}"
headers = {
          'Authorization': "{{token}}",
          'Content-Type': 'application/json'
}
print(requests.request("POST", url_issue, headers=headers, data = payload)))
Positive Point : the attachment appears in my mantis issue. But when I click on it to see the attachment, there is an error. Google Chrome can't open the pdf.

So, I try to find the problem... and i think I found it !
When I go to this website

{{url_Mantis}}/api/rest/issues/{{id_Mantis_issue}}/

It is possible to have some data about the issue. And I saw that :

Code: Select all

"attachments":[{
          "id":25,
          "reporter":{"id":4,"name":"....."},
          "created_at":"...",
          "filename":"test.pdf",
          "size":24,
          "content_type":"application\/octet-stream; charset=binary"}]
I think the problem comes from the content type (last line). It should be like this :

Code: Select all

          "content_type":"application\/pdf; charset=binary"}]
So, I change the payload like this :

Code: Select all

payload = "{\n  \"files\": [\n  \t{\n  \t\t\"name\": \"test.pdf\",\n  \t\t\"content\": \"C:/Users/afournel/Desktop/Doc7.pdf\",
                                    \n  \t\t\"content_type\":\"application\/pdf; charset=binary\"\n  \t}  \t\n  ]\n}"
With this kind of payload, the content type is still the same, and my file can't be opened. MantisBT can't understand my file is a pdf.

i would be very grateful for your help,

Alexandre