Python Forum

Full Version: Python requests writes the name of the file instead of contents to web page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a function that tries to write a web page into conflunece. Instead of posting the contents of a file, it writes the name of the file to the web page.

I am using the python requests module to write to the web page.

On the web page I see this:

../output_files/aws_instance_list/html/aws-master-list-06-05-2019.html
As the only contents of the page.

This is the code that I'm using to write to the page:
BASE_URL = "https://wiki.us.cworld.company.com/rest/api/content"
VIEW_URL = "https://wiki.us.cworld.company.com/pages/viewpage.action?pageId="
def get_page_ancestors(auth, pageid):
    # Get basic page information plus the ancestors property
    url = '{base}/{pageid}?expand=ancestors'.format(
        base = BASE_URL,
        pageid = pageid)
    r = requests.get(url, auth = auth)
    r.raise_for_status()
    return r.json()['ancestors']


def get_page_info(auth, pageid):
    url = '{base}/{pageid}'.format(
        base = BASE_URL,
        pageid = pageid)
    r = requests.get(url, auth = auth)
    r.raise_for_status()
    return r.json()
    htmlfile = '../output_files/aws_instance_list/html/aws-master-list-06-05-2019.html'
    pageid = 138317098
    auth = ('username','password')
    write_data(auth, htmlfile, pageid, 'AWS EC2 Instance List')
    def write_data(auth, htmlfile, pageid, title = None):
        info = get_page_info(auth, pageid)
        ver = int(info['version']['number']) + 1
        ancestors = get_page_ancestors(auth, pageid)
        anc = ancestors[-1]
        del anc['_links']
        del anc['_expandable']
        del anc['extensions']
        if title is not None:
            info['title'] = title
        data = {
            'id' : str(pageid),
            'type' : 'page',
            'title' : info['title'],
            'version' : {'number' : ver},
            'ancestors' : [anc],
            'body'  : {
            'storage' :
            {
                'representation' : 'storage',
                'value' : str(htmlfile),
             }
          }
        }
        data = json.dumps(data)
        url = '{base}/{pageid}'.format(base = BASE_URL, pageid = pageid)
        r = requests.put(
            url,
            data = data,
            auth = auth,
            headers = { 'Content-Type' : 'application/json' }
        )
        r.raise_for_status()
        print("Wrote '%s' version %d" % (info['title'], ver))
        print("URL: %s%d" % (VIEW_URL, pageid))
I've looked at the contents of the 'htmlfile' and can see that it contains valid HTML.

How can I write the contents of the file to the page instead of the name of the file?
this code doesn't look like it will run period, at least not do much.
line 19 is an unconditional return, so nothing beyond that will ever get executes.