문제

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

도움이 되었습니까?

해결책

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.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top