Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

XSRF Check Failed when uploading attachments to multiple items

JR Martin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 24, 2020

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;
}
}
}

 

1 answer

1 accepted

1 vote
Answer accepted
JR Martin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 25, 2020

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");

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events