JIRA API 附件名称包含发布文件的完整路径 [英] JIRA API attachment names contain the whole paths of the posted files

查看:49
本文介绍了JIRA API 附件名称包含发布文件的完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 Jira API,发现我的请求结果不一致.有时它有效,有时它不起作用.上周我可以很好地将附件发布到问题中,但现在出现了一个老问题:附件的名称包含发布文件的整个路径,因此无法打开附件.我使用 json 表示来发布文件:

I have been working with Jira API and have seen inconsistent results for my requests. Sometimes it works and sometimes it doesn't. Last week I was able to post attachments to issues just fine, but now an old problem occurred: the names of the attachments contain the whole path of the posted file, hence the attachments can't be opened. I use json representation to post files:

$array = array("file"=>"@filename");
json_encode($array);
...

这会发布文件,但问题是当它发布时,JIRA 中的文件名是这样的:

This gets the file posted but the problem is when it's posted the file names in JIRA are like this:

/var/www/user/text.text

/var/www/user/text.text

不用说在JIRA中是打不开的.我以前也遇到过这个问题,然后突然消失了,现在又出现了.我真的不明白.顺便说一句,即使文档中可能推荐使用 curl,我也没有为此请求使用 curl.

Needless to say it can't be opened in JIRA. I had this problem before, then it suddenly disappeared, now it occurred again. I don't really get it. By the way I am not using curl for this request even though it might be recommended in the documentation.

推荐答案

我意识到这个问题有点老了,但我遇到了类似的问题.似乎 Jira 不一定按预期修剪文件名.我能够使用以下方法修复它.如果您使用的是 PHP >= 5.5.0:

I realize this question is somewhat old but I had a similar problem. It seems Jira doesn't necessarily trim the filename as expected. I was able to fix it with the following. If you're using PHP >= 5.5.0:

$url = "http://example.com/jira/rest/api/2/issue/123456/attachments";
$headers = array("X-Atlassian-Token: nocheck");

$attachmentPath = "/full/path/to/file";
$filename = array_pop(explode('/', $attachmentPath));

$cfile = new CURLFile($attachmentPath);
$cfile->setPostFilename($filename);

$data = array('file'=>$cfile);

$ch = curl_init();

curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$pass");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

$result = curl_exec($ch);

$ch_error = curl_error($ch);

if ($ch_error){
    echo "cURL Error: $ch_error"; exit();
} else {
    print_r($result);
}

对于 PHP <5.5.0 但 > 5.2.10(请参阅 此错误):

$data = array('file'=>"@{$attachmentPath};filename={$filename}");

这篇关于JIRA API 附件名称包含发布文件的完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆