1

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
0

1 Answer 1

5

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)
answered Mar 17, 2013 at 14:15
Sign up to request clarification or add additional context in comments.

3 Comments

@btevfik: That is a .get() (GET request) call. For POST, use data.
why in the earth do they make them different
@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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.