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)
7 comments
Comment actions Permalink
Hi!
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?
0
Comment actions Permalink
Thank you for your help.

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
0
Comment actions Permalink
You really need to have Content-Length, as you are using Apach and it pays attention to this header. So now we sould solve the problem with 505 error. Can you somehow get representation of your http request (I mean request with all headers).
0
Comment actions Permalink
Here we go!
I did tcpdump  -n -i eth0 -s 0 -w output.txt src or dst port 8181

Host: lapdev-01.devsi.gc.priv:8181
Content-type: application/x-www-form-urlencoded
Cookie: JSESSIONID=113527F1E1A96B0C67FD5764B1E59176; Path=/youtrack
Cookie: jetbrains.charisma.main.security.PRINCIPAL=ZmQ3ZGVjYWU1Zjk3Njc2MzU4NzYyZjdhMTg5YTZkNjU4NmYyMjRmMzFjNTE3MDFjODUyNWE4MGRlMjg4YmZjMzphLmF0dGljYQ; Expires=Sat, 06-Jul-2013 10:13:00 GMT; Path=/$
Content-Length: 18
Connection: keep-alive
Content-Type: multipart/form-data; boundary=-----0c36d1d

-----0c36d1d
Content-Disposition: form-data; name="test.txt"; filename="test.txt"
Content-Type: binary
Content-Transfer-Encoding: Base64

SEVMTE8gV09STEQ=

-----0c36d1d--
prettyPrint();


[?TTP/1.1 204 No Content
Server: Apache-Coyote/1.1
Cache-Control: no-cache, no-store, no-transform, must-revalidate
Date: Fri, 06 Jul 2012 10:13:00 GMT
Connection: keep-alive

HTTP/1.1 505 HTTP Version Not Supported
Server: Apache-Coyote/1.1
Date: Fri, 06 Jul 2012 10:13:00 GMT
Connection: close
prettyPrint();
0
Comment actions Permalink
@batataw, did you try making other requests to REST API?
0
Comment actions Permalink
Yes I did, the authentification, users list, issues list or create an issue work perfectly.
Attach file does not.
0
Comment actions Permalink
Can you try, f.e. "Create bundle" http://confluence.jetbrains.net/display/YTD4/PUT+Enumeration - I just want to check if you can't perform any request with content.
0

Please sign in to leave a comment.