Question

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

Était-ce utile?

La 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.

Autres conseils

In general, inside the programming, when using the method POST there is a "multipart/form-data". Inside the form with method POST there is a enctype="multipart/form-data" when you send a image or a file.

You can try:

 POST /add HTTP/1.1   
 Content-Type: multipart/form-data; boundary=---------------------------41184676334
 Content-type:application/x-http-form-urlencoded 
 Content-Length: 3 //is better that you put a 216
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top