How to attach a file to an issue (REST API)
I did try to attach a file through the REST API, it did not work at all.
I've tried the 2 functions that I found:
POST /issue/{issue}/attachment
POST /import/{issue}/attachment
Thank you for your help
PHP
$data = 'HELLO WORLD';
$content = chunk_split(base64_encode($data));
$issue = 'TEST-158';
$boundary = substr( md5(uniqid(rand())),0, 7);
$file = 'test.txt';
$params[] = 'Connection: keep-alive';
$params[] = 'Content-Type: multipart/form-data; boundary=
' . $boundary;
$body = '
'. $boundary. "\r\n";
$body .= 'Content-Disposition: form-data; name="' . $file . '"; filename="' . $file . '"'. "\r\n";
$body .= 'Content-Type: binary'. "\r\n";
$body .= 'Content-Transfer-Encoding: Base64'. "\r\n";
$body .= "\r\n";
$body .= $content. "\r\n";
$body .= '
' . $boundary . '–'. "\r\n";
$fields = array(
'authorLogin' => 'test',
'created' => time(),
);
$url = 'http://lapdev-01.devsi.gc.priv:8181/youtrack/rest/issue/'.$id.'/attachment?'.http_build_query($fields);
$ret = $rest->setUrl($url)->setHeader($params)->post($body);
RECEIVED FROM YOUTRACK
array(2) {
["header"]=>
array(10) {
["wrapper_data"]=>
array(5) {
[0]=>
string(23) "HTTP/1.1 204 No Content"
[1]=>
string(25) "Server: Apache-Coyote/1.1"
[2]=>
string(64) "Cache-Control: no-cache, no-store, no-transform, must-revalidate"
[3]=>
string(35) "Date: Thu, 05 Jul 2012 06:59:25 GMT"
[4]=>
string(22) "Connection: keep-alive"
}
["wrapper_type"]=>
string(4) "http"
["stream_type"]=>
string(14) "tcp_socket/ssl"
["mode"]=>
string(1) "r"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
["uri"]=>
string(115) "http://lapdev-01.devsi.gc.priv:8181/youtrack/rest/issue/TEST-158/attachment?authorLogin=test&created=1341471565"
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(true)
}
["content"]=>
string(0) ""
}
output.txt (3.8KB)
I've tried the 2 functions that I found:
POST /issue/{issue}/attachment
POST /import/{issue}/attachment
Thank you for your help
PHP
$data = 'HELLO WORLD';
$content = chunk_split(base64_encode($data));
$issue = 'TEST-158';
$boundary = substr( md5(uniqid(rand())),0, 7);
$file = 'test.txt';
$params[] = 'Connection: keep-alive';
$params[] = 'Content-Type: multipart/form-data; boundary=
' . $boundary;
$body = '
'. $boundary. "\r\n";
$body .= 'Content-Disposition: form-data; name="' . $file . '"; filename="' . $file . '"'. "\r\n";
$body .= 'Content-Type: binary'. "\r\n";
$body .= 'Content-Transfer-Encoding: Base64'. "\r\n";
$body .= "\r\n";
$body .= $content. "\r\n";
$body .= '
' . $boundary . '–'. "\r\n";
$fields = array(
'authorLogin' => 'test',
'created' => time(),
);
$url = 'http://lapdev-01.devsi.gc.priv:8181/youtrack/rest/issue/'.$id.'/attachment?'.http_build_query($fields);
$ret = $rest->setUrl($url)->setHeader($params)->post($body);
RECEIVED FROM YOUTRACK
array(2) {
["header"]=>
array(10) {
["wrapper_data"]=>
array(5) {
[0]=>
string(23) "HTTP/1.1 204 No Content"
[1]=>
string(25) "Server: Apache-Coyote/1.1"
[2]=>
string(64) "Cache-Control: no-cache, no-store, no-transform, must-revalidate"
[3]=>
string(35) "Date: Thu, 05 Jul 2012 06:59:25 GMT"
[4]=>
string(22) "Connection: keep-alive"
}
["wrapper_type"]=>
string(4) "http"
["stream_type"]=>
string(14) "tcp_socket/ssl"
["mode"]=>
string(1) "r"
["unread_bytes"]=>
int(0)
["seekable"]=>
bool(false)
["uri"]=>
string(115) "http://lapdev-01.devsi.gc.priv:8181/youtrack/rest/issue/TEST-158/attachment?authorLogin=test&created=1341471565"
["timed_out"]=>
bool(false)
["blocked"]=>
bool(true)
["eof"]=>
bool(true)
}
["content"]=>
string(0) ""
}
output.txt (3.8KB)
Please sign in to leave a comment.
Please, check that Content-Length header is set properly in your request (I do not know whether PHP sets it automatically). Also, could you please tell how do you run YouTrack: jar file, Windowns service or war file? If it's war file - what servlet container do you use?
I did remove the length field because I get an error message immediatly:
$params[] = 'Content-Length: ' . strlen($content);
HTTP/1.1 505 HTTP Version Not Supported
Server: Apache-Coyote/1.1
Date: Thu, 05 Jul 2012 16:04:18 GMT
Connection: close
Youtrack is installed on Debian / Tomcat6 / WAR file
I did tcpdump -n -i eth0 -s 0 -w output.txt src or dst port 8181
Attach file does not.