2016-12-28 11 views
2

Jira REST API(バージョン6.4.3)に添付ファイルを追加しようとしています。 Jira 6.4.3 DocumentationJiraで残りのAPIに添付ファイルをポストする6.4.3

ここでの問題は、いくつかのエラーが発生した後、レスポンスコードHTTP/1.1 OKを取得できたが、jira apiに記載されているデータがなく、添付ファイルが投稿されていないということです。ここで

は、完全なTCPストリームである:

POST /rest/api/2/issue/SANDBOX-106/attachments HTTP/1.1 
X-Atlassian-Token: nocheck 
Content-Type: multipart/form-data; boundary="3555f78b-ebcc-406a-ab5a-8bf9009d7254" 
Host: ****** 
Cookie:***** 
Content-Length: 236 
Expect: 100-continue 

HTTP/1.1 100 Continue 

--3555f78b-ebcc-406a-ab5a-8bf9009d7254 
Content-Disposition: file; filename=huowzsqn.hpi 

......... 
.. 
.................. !"#$%&'()*+,-./:;<=>[email protected][\]^_`abc 
--3555f78b-ebcc-406a-ab5a-8bf9009d7254-- 
HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1 
X-AREQUESTID: 650x4885x1 
X-ASESSIONID: 9sodh7 
X-ASEN: SEN-4692241 
X-Seraph-LoginReason: OK 
Set-Cookie: **** 
X-AUSERNAME: **** 
Cache-Control: no-cache, no-store, no-transform 
X-Content-Type-Options: nosniff 
Content-Type: application/json;charset=UTF-8 
Transfer-Encoding: chunked 
Date: Wed, 28 Dec 2016 09:50:39 GMT 

2 
[] 
0 

JIRA APIに投稿するための方法:

protected async Task<CQ> preformRequest(string path, FileParamter file, Dictionary<int, Exception> exceptions = null, Dictionary<String, String> headers = null, string dispositionType = "file") 
     { 
      if (!checkUrl(path)) 
      { 
       throw new ArgumentException("Not a valid url."); 
      } 

      MultipartFormDataContent multiPartContent = new MultipartFormDataContent(); 

      this.addHeaders(headers); 

      ByteArrayContent fileContent = new ByteArrayContent(file.File); 

      fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(dispositionType) 
      { 
       FileName = file.FileName 

      }; 

      multiPartContent.Add(fileContent); 

      var task = Client.PostAsync(path, multiPartContent) 
       .ContinueWith(tsk => 
       { 
        HttpStatusCode code = tsk.Result.StatusCode; 

        if (!this.isStatusCodeSuccess(code)) 
        { 
         this.handleStatusCodes(code, exceptions); 
        } 

        return tsk.Result.Content.ReadAsStringAsync(); 
       }); 


      return await Task.Factory.StartNew(() => CQ.Create(task.Unwrap().Result)); 
     } 

FileParamterクラス:生成するための

public class FileParamter 
    { 
     public byte[] File { get; set; } 
     public string FileName { get; set; } 
     public FileParamter(byte[] file) : this(file, null) { } 

     public FileParamter(byte[] file, string fileName) 
     { 
      this.File = file; 
      this.FileName = fileName; 
     } 

     public FileParamter() 
     { 

     } 
    } 

テストクラスランダムファイル:

public FileParamter createIssueAttachment() 
     { 
      string filePath = 
       System.IO.Path.Combine(
        System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), 
        System.IO.Path.GetRandomFileName()); 

      //filePath = Path.ChangeExtension(filePath, "txt"); 

      if (!System.IO.File.Exists(filePath)) 
      { 
       using (System.IO.FileStream fs = System.IO.File.Create(filePath)) 
       { 
        for (byte i = 0; i < 100; i++) 
        { 
         fs.WriteByte(i); 
        } 
       } 
      } 

      FileInfo info = new FileInfo(filePath); 

      byte[] file = File.ReadAllBytes(filePath); 

      FileParamter fileParamter = new FileParamter 
      { 
       File = file, 
       FileName = info.Name 
      }; 

      this.deleteFile(filePath); 

      return fileParamter; 
     } 

答えて

2

他の誰かがこの問題に遭遇した場合、ファイルヘッダーのコンテンツの配置に問題があります。

それはでした:

Content-Disposition: file; filename=huowzsqn.hpi 

そして、次のようになります。

Content-Disposition: form-data; filename=huowzsqn.hpi; name=file 

加工方法:

protected async Task<CQ> preformRequest(string path, FileParamter file, Dictionary<int, Exception> exceptions = null, Dictionary<String, String> headers = null, string dispositionType = "file") 
     { 
      if (!checkUrl(path)) 
      { 
       throw new ArgumentException("Not a valid url."); 
      } 

      MultipartFormDataContent multiPartContent = new MultipartFormDataContent(); 

      this.addHeaders(headers); 

      ByteArrayContent fileContent = new ByteArrayContent(file.File); 

      fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue(dispositionType) 
      { 
       FileName = file.FileName, 
       Name = dispositionType, 
       DispositionType = "form-data" 
      }; 

      multiPartContent.Add(fileContent); 

      var task = Client.PostAsync(path, multiPartContent) 
       .ContinueWith(tsk => 
       { 
        HttpStatusCode code = tsk.Result.StatusCode; 

        if (!this.isStatusCodeSuccess(code)) 
        { 
         this.handleStatusCodes(code, exceptions); 
        } 

        return tsk.Result.Content.ReadAsStringAsync(); 
       }); 


      return await Task.Factory.StartNew(() => CQ.Create(task.Unwrap().Result)); 
     } 
関連する問題