It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How do I attach a document in confluence using Python with an Oauth Token?

mark mark May 03, 2017

I would like to attach files to confluence from the rest API using Python with Oauth.

I was trying with the code below but I get the following error message

Unsupported Media Type

description

The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

fileinfo = open(filename, 'rb')
files = {
  'file': fileinfo}
headers = {'X-Atlassian-Token': 'no-check'}
headers['Content-Type'] = 'text/plain'
url = self._get_url('content/'+str(contentId) + "/child/attachment")
r = self._session.post(
            url, files=files, headers=headers)

#Where _session is my ResilientSession with Oauth info already setup

1 answer

1 accepted

0 votes
Answer accepted
mark mark May 05, 2017

I was able to get this to work with the following code modified from the jira client, that came from pip install jira.

 def content_attachment(self, contentId, filename):
        url = self._get_url('content/'+str(contentId) + "/child/attachment")
        #attachment will also work if you just pass the filename
        attachment = open(filename, 'rb')
        result = None
        try:
            if 'MultipartEncoder' not in globals():
                #method = 'old'
                result = self._session.post(
                    url,
                    files={
                        'file': (filename, attachment, 'application/octet-stream')},
                    headers=CaseInsensitiveDict({'content-type': None, 'X-Atlassian-Token': 'nocheck'}))
            else:
                #method = 'MultipartEncoder'    
                def file_stream():
                    return MultipartEncoder(
                        fields={
                            'file': (filename, attachment, 'application/octet-stream')})
                m = file_stream()
                result = self._session.post(
                    url, data=m, headers=CaseInsensitiveDict({'content-type': m.content_type, 'X-Atlassian-Token': 'nocheck'}), retry_data=file_stream)
        except JIRAError as e:
            if e.status_code == 400:
                logging.error(str(e))
                return
            raise
        if not result or not isinstance(result, collections.Iterable):
            raise JIRAError("Unable to parse JSON: %s" % result)

        for data in result:
            print (data)
            for fields in result.get(data):
                print (fields)
                return fields.get("id")

        return json_loads(result)

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Confluence

6 Awesome Ways to Apply Trello, JIRA and Confluence to your Project

I attended  Atlassian Summit 2019  and learned a lot from the presenters, attendees and knowledgeable Atlassian product managers. The presentations I attended focused on applying Agile, pla...

2,055 views 11 27
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you