I have a C# project that I'm working on that integrates items between JIRA and Azure Devops (ADO). On the piece that uploads ADO attachments into their associated JIRA items, it'll successfully upload the attachments for the first item, but any subsequent items will fail. The error is XSRF Check Failed. This is the code I'm using to upload the attachments:
public async Task<List<JIRAWorkItemModel.Attachment>> JiraUploadAttachment(string jiraId, List<byte[]> contentList, List<string> fileNameList)
{
var enumerator = 0;
using (MultipartFormDataContent multiPartContent = new MultipartFormDataContent(string.Format("----boundary{0}", jiraId)))
{
foreach (var fileName in fileNameList)
{
ByteArrayContent byteArrayContent = new ByteArrayContent(contentList[enumerator]);
multiPartContent.Add(byteArrayContent, "file", fileName);
enumerator++;
}
return await JiraPostAttachment(jiraId, multiPartContent);
}
}
public async Task<List<JIRAWorkItemModel.Attachment>> JiraPostAttachment(string jiraId, MultipartFormDataContent multiPartContent, int runCount = 0)
{
string apiUri = JiraApiUris.AddAttachment(jiraId);
HttpClient client = IntegrationHttpClient;
client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check");
try
{
Logger.Log($@"Initiating POST request to uri {client.BaseAddress}{apiUri}", 3, VerboseLogging);
using (var response = await client.PostAsync(apiUri, multiPartContent))
{
Logger.Log(@$"Response Status Code: {(int)response.StatusCode}", 4, VerboseLogging);
string responseBody = await response.Content.ReadAsStringAsync();
Logger.Log($@"Post Response Body: {responseBody}");
var request = JsonSerializer.Deserialize<List<JIRAWorkItemModel.Attachment>>(responseBody, options);
return request;
}
}
}
public async Task<T> PostRequest<T>(string apiUri, object content, bool isADORequest, string mediaType = "application/json", int runCount = 0)
{
string jsonContent;
if (content.GetType() == typeof(string))
{
jsonContent = (string)content;
}
else
{
jsonContent = JsonSerializer.Serialize(content,options);
}
StringContent stringContent = new StringContent(jsonContent, Encoding.UTF8, mediaType);
HttpClient client = ADOHttpClient;
if(!isADORequest)
{
client = IntegrationHttpClient;
}
try
{
Logger.Log($@"Initiating POST request to uri {client.BaseAddress}{apiUri}", 3, VerboseLogging);
using (var response = await client.PostAsync(apiUri, stringContent))
{
Logger.Log(@$"Response Status Code: {(int)response.StatusCode}", 4, VerboseLogging);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();
if (typeof(T) == typeof(string))
{
return (T)(object)response.StatusCode.ToString();
}
var request = JsonSerializer.Deserialize<T>(responseBody, options);
return request;
}
}
}
I figured it out. I needed to change client.DefaultRequestHeaders.Add("X-Atlassian-Token", "no-check"); to multiPartContent.Headers.Add("X-Atlassian-Token", "no-check");
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.