Search code examples
pythonposttelnet

Python - get values from POST via Telnet


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=['GET', 'POST'])
def add_entry():
    db = get_db()
    data = request.args.get('title', '')
    if not data:
        data = "wrong"
    return data

When I use telnet command like this:

$ GET /add?title=foo&text=baz+and+spam+and+eggs HTTP/1.0

The response is correct:

HTTP/1.0 200 OK
Content-Type: text/html; charset=utf-8
Content-Length: 3
Server: Werkzeug/0.9.4 Python/3.3.3
Date: Sun, 16 Feb 2014 12:31:50 GMT

foo

But I need send values via POST and I don't how can I get them from POST body. If I try someting like this:

$ POST /add HTTP/1.1
$ Content-type:application/x-http-form-urlencoded    
$ title=foo&text=baz+and+spam+and+eggs

And the same python method, the result will be "wrong". I'm using Flask framework. It is my homework so I can't use curl, only telnet and because I'm testing only server side I can't use any form. So I would like to ask how can I get values from POST to the variable data in python script. Thank you


Solution

  • On HTTP you have an empty line between header and data. You post should look like:

    POST /add HTTP/1.1
    Content-type:application/x-http-form-urlencoded
    
    title=foo&text=baz+and+spam+and+eggs
    

    You might also need to send the content-length header.