sending attachment via post

Get help from other users here.

Moderators: Developer, Contributor

Post Reply
PacoA
Posts: 8
Joined: 12 Nov 2020, 09:31

sending attachment via post

Post by PacoA »

Hello.
I´ve connected Mantis and Jira with API´s. It works.
But now I cannot send a bug attachemt file from Mantis to Jira via POST.
I´m testing it with Postman and it works. When I create the cURL request with postman, I only have to select the file. When doing it in Mantis I retrieve the file from mantis_bug_file_table where I can find filename, file_type and filesize. But in column diskfile I only have a hexadecimal string that I don´t know how to manage and the column folder is empty.

How can I get the path and use it on the cURL request?

Thanks
Paco
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: sending attachment via post

Post by cas »

Think you have stored the files in the database. Via the admin section, you can move them to a file location :mrgreen:
PacoA
Posts: 8
Joined: 12 Nov 2020, 09:31

Re: sending attachment via post

Post by PacoA »

Think you have stored the files in the database.
Attachments in bugnotes are stored in mantis_bug_file_table.

What I´m trying to do is write code in a plugin in order to, automatically, the attachments will be sent to Jira by cURL post.
Acctually this plugin is working updating status, some fields and bugnotes (only text) in Jira when users do it in Mantis. But I don´t know how to do it with attachments.

Thanks
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: sending attachment via post

Post by cas »

All required functions you can find in core/file_api.php .
PacoA
Posts: 8
Joined: 12 Nov 2020, 09:31

Re: sending attachment via post

Post by PacoA »

cas wrote: 14 Dec 2020, 19:29 All required functions you can find in core/file_api.php .
I thought so, but I have not found how to do it.
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: sending attachment via post

Post by cas »

Well in the end you need to download the file and attach to your mail.
Not sure if you would like to bypass all authorization checks in Mantis itself but in principle you can get this done by calling this program:
file_download.php?file_id=' . $t_id . '&type=bug'
Once you have file_id of the attachment, you are in business :mrgreen:
PacoA
Posts: 8
Joined: 12 Nov 2020, 09:31

Re: sending attachment via post

Post by PacoA »

I must not have explained my problem well. Its nothing about sending mails.

It is about send the mantis bug files of an issue to another issue in another system, like I usually do with bugs itselves or bugnotes (test), etc.

Postman generate this code when testing Jira API. And it works:

Code: Select all

<?php

$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "https://devops.myorg.es/dev-jira/rest/api/2/issue/$JIRAid/attachments",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"file\"; filename=\"Captura.JPG\"\r\nContent-Type: image/jpeg\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--",
  CURLOPT_HTTPHEADER => array(
    "Accept: */*",
    "Accept-Encoding: gzip, deflate",
    "Authorization: Basic authorizationommited",
    "Cache-Control: no-cache",
    "Connection: keep-alive",
    "Content-Length: 76315",
    "Content-Type: application/x-www-form-urlencoded",
    "Cookie: JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; atlassian.xsrf.token=xxxx-xxxx-xxxx-xxxx_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_lin; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyy",
    "Host: devops.myorg.es",
    "Postman-Token: generated by postman",
    "User-Agent: PostmanRuntime/7.19.0",
    "X-Atlassian-Token: no-check",
    "cache-control: no-cache",
    "content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}
I usually copy this generated code in my php plugin in Mantis and it works (here there are some hidden items).


But with bug_files, as seen in the example, Postman only generates the name of the file ("Captura.JPG" in the example) and it works. Not in my code.
I think I must add the complete path to the file but I cannot find it in Mantis (netither in 'file_api.php', nor in database)
cas
Posts: 1586
Joined: 11 Mar 2006, 16:08
Contact:

Re: sending attachment via post

Post by cas »

if you download the attachment to a temp location, you have the correct path where it is stored. Does not matter if you send it by mail or send a curl link.
So download the file using file_download.php and point you cUrl to that location.
After confirmation that is has been transmitted, you can add cleanup activities.
Post Reply