login community faq

tl;dr: how do I handle POST requests in my cherrypy web app?

I'm trying to build a simple API in CherryPy that takes POST requests from an iPhone app and returns JSON. I'm converting an old PHP project, so PHP is what I'm familiar with. I'm new to Python.

I'm trying to test my script's handling of POST requests like this:

curl -d "key1=value1" http://<username>.webfactional.com/

When I curl without the -d, the return value loads fine; however, when I curl -d, I get a 400 Bad Request: Bad syntax or unsupported method. I don't know the "right" way to handle a POST request, so I'm trying one I found at http://stackoverflow.com/a/464977/255489:

from cherrypy import request
    # Root class, index() method...
    return request.params['key1']

But this gives me a 500 Internal Server Error without passing any parameters, and 400 Bad Request with parameters passed with POST.

Edit as per Johns's question:

When I curl without any -d parameters I get this in the access log:

{my IP address} - - [16/Apr/2012:00:05:18 -0500]
"GET / HTTP/1.1" 500 805 "-" "curl/7.21.4 (universal-apple-darwin11.0)
libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5"

and this in the error log:

2012/04/16 00:05:19 [info] 20703#0: *123544253 client {my IP address}
closed keepalive connection

Hope this helps.

asked Apr 15 '12 at 23:19

ZevEisenberg's gravatar image

ZevEisenberg
74

edited Apr 16 '12 at 00:07


Found some useful documentation here.

Basically you must declare the objects as keyword arguments in the function.

The function should look something like this,

1
2
3
4
5
class Root:
    def index(self, key1=None):
        return key1

    index.exposed = True

answered Apr 16 '12 at 19:31

johns's gravatar image

johns ♦♦
345427

Hooray! That worked for both GET and POST. Thanks!

(Apr 16 '12 at 20:24) ZevEisenberg ZevEisenberg's gravatar image

The 500 error should be logged in ~/logs/frontend/. What does the error log say about the 500 error?

answered Apr 15 '12 at 23:51

johns's gravatar image

johns ♦♦
345427

Updated the question with what I found in the logs. Hope it helps.

(Apr 16 '12 at 00:04) ZevEisenberg ZevEisenberg's gravatar image

I looked at your code on the server, you currently have the line which intercepts the post commented out. Could you remove the comment so we could take a look with it enabled?

(Apr 16 '12 at 02:02) johns ♦♦ johns's gravatar image

I've uncommented that line (and cleaned up the code a bit so you can see what I'm doing). It now gets a 500 on curl and a 400 on curl -d.

(Apr 16 '12 at 19:00) ZevEisenberg ZevEisenberg's gravatar image
Your answer
If you have an answer to the above question, then use the form below. Otherwise, use the appropriate 'add new comment' button above to post your feedback.
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×43
×19
×7
×5
×4

Asked: Apr 15 '12 at 23:19

Seen: 1,661 times

Last updated: Apr 16 '12 at 20:24

Plans & prices    Sign up    Why WebFaction?    Contact us    Affiliate program    Support    Legal    Jobs    Blog    Control panel login
Powered by OSQA
© Copyright 2003-2012 Swarma Limited - WebFaction is a service of Swarma Limited