system.net.httpClient file parameter for Jira attachment

Hi,
I'm trying to post attachment to Jira Sofware Cloud using system.net.httpClient

Jira documentation :
(https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post)

I'm using the following code :

def addAttachment(domain, filepath, issue, user, passwd):
baseurl	= "https://" + domain + "/" + str(issue) + "/attachments"

headers = {
   "Accept": "application/json",
   "X-Atlassian-Token": "no-check",
}

payload = filepath

client    = system.net.httpClient()
response = client.post(baseurl, username=user, password=passwd, file=payload, headers=headers)

if response.good:
	return str(response) + " | " + response.text
else :
	return str(response) + " | " + response.text

resulting in the following error :

..atlassian.net/rest/api/3/issue/10334/attachments' [400]> | {"message":"org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/octet-stream","status-code":500,"stack-trace":""}

It seems like the file parameter is not fully compliant with what Jira is waiting for.

Could you help me solving this issue ?

Thank you in advance

Guillaume

Tip: use the </> code formatting button to preserve code indentation and apply syntax highlighting. It's essential for Python and makes any code much easier to read. There's an edit button 🖉 below your post so you can fix it.

1 Like

httpClient doesn't currently have any builtin support for form data requests, unfortunately.

You'll have to manually create the data in the appropriate 'shape' - basically, write out the required header and then append the encoded attachment data, and set the Content-Type header manually.