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
asked Mar 17, 2013 at 14:05
Stormvirux
9093 gold badges18 silver badges35 bronze badges
1 Answer 1
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)
Sign up to request clarification or add additional context in comments.
3 Comments
Martijn Pieters
@btevfik: That is a
.get() (GET request) call. For POST, use data.btevfik
why in the earth do they make them different
Anorov
@btevfik Because GET parameters are completely different from POST data? A POST request can optionally specify GET parameters as well as POST data, though a GET request obviously cannot specify POST data. They're different things and used in different situations.
Explore related questions
See similar questions with these tags.
default