Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to download files in python with post request?

Tags:

python

I am trying to download an export from an app automatically with python. Here is my code:

export_url = 'https://....'
payload = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
r = requests.post(export_url, data=payload)
print(r)

the response is 200 but the file is missing somehow what is wrong?

edit:

this is my whole code:

import requests

URL = 'homepage_after_login'
LOGIN_URL = 'loginpage'

session = requests.session()

username = 'uname'
password = 'pass'
loginformtype = "value"
submit = "Sign+in"

login_data = {'username'        : username,
              'password'        : password,
              'login-form-type' : loginformtype,
              'submit'          : submit}

session.post(LOGIN_URL, data=login_data)

req = session.get(URL)

export_url = 'https://....'
payload = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
r = requests.post(export_url, data=payload)
print(r)

possibily it ignores the login part?

like image 830
gyld Avatar asked Jun 03 '26 10:06

gyld


1 Answers

Perhaps you should take a look at the documentation.

When you print r, you get the status code. (Well, not exactly, but the printed number is the status code.)

What you probably want to use is r.text:

text

Content of the response, in unicode.

If Response.encoding is None, encoding will be guessed using chardet.

The encoding of the response content is determined based solely on HTTP headers, following RFC 2616 to the letter. If you can take advantage of non-HTTP knowledge to make a better guess at the encoding, you should set r.encoding appropriately before accessing this property.

Or r.content:

content

Content of the response, in bytes.

like image 131
Yuriko Avatar answered Jun 04 '26 22:06

Yuriko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!