Stormvirux
Stormvirux

Reputation: 909

Post method in Python using requests

I have simple form which accepts text and calls a php file abc.php through post method:

<form method='post' action="abc.php">
<input type="textarea" name="text">
<input type="submit">
</form>

contents of abc.php

<?php
$msg=$_POST["text"];
print $msg;
?>

I wrote a python script for posting messages using requests library as follows:

import requests
keys={'text':'lol rofl'}
r=requests.post("http://127.0.0.1/abc.php/POST",params=keys)
print r.url
print r.text

I was getting the following output(error):

  http://localhost/abc.php/POST?text=lol+rofl

<b>notice</b>:undefined index;text in C:/xampp/htdocs/abc.php on line 2

note:form and abc.php works perfectly when run from the browser

Upvotes: 1

Views: 15659

Answers (1)

t-8ch
t-8ch

Reputation: 2713

The right parameter to use is data:

http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

Typically, you want to send some form-encoded data — much like an HTML form.
To do this, simply pass a dictionary to the data argument.
Your dictionary of data will automatically be form-encoded
when the request is made:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)

Upvotes: 5

Related Questions