Reputation: 4838
I'm using localhost to testing python scripts and I need to test it via telnet, where I'm using PuTTY. I have this python script:
@app.route('/add/', methods=['POST'])
def add_entry():
db = get_db()
db.execute('insert into entries (title, text) values (?, ?)',
("value_from_telnet", "another_value_from_telnet"))
db.commit()
print("Saved")
return "Saved"
And now I don't know how to connect to 127.0.0.1/add and how put values into POST method. I can't use connection like this:
$ o 127.0.0.1/add
And if I try this:
$ o 127.0.0.1
$ GET / HTTP/1.0
$ Host: 127.0.0.1/add
then the result is 404 NOT FOUND (via browser it works, but only with static insert values and GET method). So I would like to ask how can I connect to this address 127.0.0.1/add and get values into POST method to use them as insert values to the database? Thank you
Upvotes: 0
Views: 756
Reputation: 9333
use curl instead : curl -v -X POST 'http://<ip.address.of.server>/add' --data "title=foo&text=baz and spam and eggs"
for a post in telnet you need to specify the body of the request like this :
POST /add HTTP/1.1
Content-type:application/x-http-form-urlencoded
title=foo&text=baz+and+spam+and+eggs
If you want to build a query string from a dictionary you need urllib
import urllib
params = {title:"foo",text:"baz and spam and eggs"}
print """
POST /add HTTP/1.1
Content-type:application/x-http-form-urlencoded
""",urlencode(params)
Upvotes: 2