Valdemar Z
Valdemar Z

Reputation: 91

Python post urlencoded from file

When i trying to post data from file

headers = {'content-type': 'text/plain'}
files = {'Content': open(os.getcwd()+'\\1.txt', 'rb')}

contentR = requests.post("http://" + domain +"index.php", params=payload, data=files, headers=headers)

I get something like urlencoded string

Content=%3C%3Fphp+echo%28%22Hello+world%22+%29%3B+%3F%3E

instead of

<?php echo("Hello world" ); ?>

How to post data as is in text file?

Upvotes: 0

Views: 186

Answers (1)

Ian Stapleton Cordasco
Ian Stapleton Cordasco

Reputation: 28807

If you read the docs closely, you can either use the file object:

headers = {'content-type': 'text/plain'}
file = open(os.getcwd()+'\\1.txt', 'rb')

contentR = requests.post("http://" + domain +"index.php", params=payload, data=file, headers=headers)

Or you can just pass a string with the file contents like so:

file_contents = open(os.getcwd() + '\\`.txt', 'rb').read()
contentR = requests.post("http://" + domain +"index.php", params=payload, data=file_contents, headers=headers)

If you pass a dictionary it will always be urlencoded so that is not what you want to do.

Upvotes: 2

Related Questions