2
我正在嘗試向Jira REST API添加附件(版本6.4.3)。 Jira 6.4.3 Documentation使用Jira發佈附件到Rest 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;
}