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


Messages In This Thread
Python requests writes the name of the file instead of contents to web page - by bluethundr - Jun-05-2019, 04:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Connecting to Remote Server to read contents of a file ChaitanyaSharma 1 191 May-03-2024, 07:23 AM
Last Post: Pedroski55
  Python logging RotatingFileHandler writes to random file after the first log rotation rawatg 0 445 Feb-15-2024, 11:15 AM
Last Post: rawatg
  Python SSL web page scraping Vadanane 1 956 Jan-13-2023, 04:11 PM
Last Post: snippsat
  python requests library .JSON() error mHosseinDS86 6 3,506 Dec-19-2022, 08:28 PM
Last Post: deanhystad
  writelines only writes one line to file gr3yali3n 2 2,426 Dec-05-2021, 10:02 PM
Last Post: gr3yali3n
  Python requests oauth2 access token herobpv 6 4,015 Sep-27-2021, 06:54 PM
Last Post: herobpv
  Python Requests SSL Aussie 0 2,009 Jan-07-2021, 02:09 AM
Last Post: Aussie
  Python Requests Aussie 2 2,781 Dec-23-2020, 03:24 AM
Last Post: Aussie
  Python Requests package: Handling xml response soumyarani 1 2,196 Sep-14-2020, 11:41 AM
Last Post: buran
  Output CSV file with filepath and file contents glittergirl 1 1,773 Aug-03-2020, 01:50 AM
Last Post: glittergirl

Forum Jump:

User Panel Messages

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