Uploading files to JIRA through REST API with HttpWebRequest

I’m trying to upload files to a JIRA issue using C# and HttpWebRequest but getting a 404 error. According to the JIRA REST API docs, I need to send a multipart POST request with the X-Atlassian-Token header set to “nocheck”. Here’s what I have so far:

foreach (JiraFileUpload upload in fileUploads.items)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(
        connectionInfo.GetBaseUrl() + "/rest/api/2/issue/" + issueKey + "/attachments"
    );
    webRequest.Headers.Add("Authorization: Basic " + connectionInfo.GetAuthToken());
    webRequest.Method = "POST";
    webRequest.ContentType = "multipart/form-data";
    webRequest.Headers.Add("X-Atlassian-Token: nocheck file=@" + Path.GetFullPath(@"..\Files\" + upload.name));
    webRequest.KeepAlive = true;
    webRequest.Proxy = proxySettings;
    
    HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
    Stream responseStream = webResponse.GetResponseStream();
    FileStream fileStream = new FileStream(Path.GetFullPath(@"..\Files\" + upload.name), FileMode.Open);
    
    byte[] buffer = new byte[256];
    int bytesRead = fileStream.Read(buffer, 0, buffer.Length);
    while (bytesRead > 0)
    {
        responseStream.Write(buffer, 0, bytesRead);
        bytesRead = fileStream.Read(buffer, 0, buffer.Length);
    }
    
    fileStream.Close();
    responseStream.Close();
    webResponse.Close();
}

The API expects multipart/form-data format with the file parameter name being “file”. What am I doing wrong here?

You’re writing to the response stream instead of the request stream. Write the file data to the request stream and ensure your multipart form data body is set up correctly. The X-Atlassian-Token header should simply be “nocheck” - avoid adding file details to it. Additionally, include the proper multipart boundaries and set the Content-Length header, as some JIRA instances may require it. By implementing these adjustments, your uploads should function without issues.

u might be mixing up the streams! you shuld write to the request stream, not the response. also, ur multipart boundary is missing - just setting content-type won’t cut it. try using WebClient or HttpClient, they’re way simpler for file uploads.

Your main problem is you’re building the multipart form data wrong. You can’t just set Content-Type to multipart/form-data without properly formatting the body with boundaries. I ran into this same issue with JIRA attachments before. You’ve got to manually build the multipart content with proper boundary strings, field names, and file headers. Don’t forget to set Content-Length before writing to the request stream. The X-Atlassian-Token header looks right, but double-check your auth token is base64 encoded correctly. I’d recommend switching to HttpClient with MultipartFormDataContent instead of HttpWebRequest - it handles the multipart formatting automatically and you’ll hit fewer bugs.