Python Forum
Python requests writes the name of the file instead of contents to web page
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python requests writes the name of the file instead of contents to web page
#1
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?
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 341 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Python SSL web page scraping Vadanane 1 874 Jan-13-2023, 04:11 PM
Last Post: snippsat
  python requests library .JSON() error mHosseinDS86 6 3,240 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  writelines only writes one line to file gr3yali3n 2 2,295 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  Python requests oauth2 access token herobpv 6 3,778 Sep-27-2021, 06:54 PM
Last Post: herobpv
  Python Requests SSL Aussie 0 1,960 Jan-07-2021, 02:09 AM
Last Post: Aussie
  Python Requests Aussie 2 2,690 Dec-23-2020, 03:24 AM
Last Post: Aussie
  Python Requests package: Handling xml response soumyarani 1 2,102 Sep-14-2020, 11:41 AM
Last Post: buran
  Output CSV file with filepath and file contents glittergirl 1 1,707 Aug-03-2020, 01:50 AM
Last Post: glittergirl
  Python API and requests deep_logic 9 4,181 Jul-29-2020, 03:47 PM
Last Post: ndc85430

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020