2012-01-11 10 views
5

Io sono nel processo di scrittura di un proof of concept server di RESTful utilizzando web.pyProof of concept server di RESTful Python (usando web.py) + test con l'arricciatura

Ecco lo script:

#!/usr/bin/env python 
import web 
import json 


def notfound(): 
    #return web.notfound("Sorry, the page you were looking for was not found.") 
    return json.dumps({'ok':0, 'errcode': 404}) 

def internalerror(): 
    #return web.internalerror("Bad, bad server. No donut for you.") 
    return json.dumps({'ok':0, 'errcode': 500}) 


urls = (
    '/(.*)', 'handleRequest', 
) 


app = web.application(urls, globals()) 
app.notfound = notfound 
app.internalerror = internalerror 


class handleRequest: 
    def GET(self, method_id): 
     if not method_id: 
      return web.notfound() 
     else: 
      return json.dumps({'ok': method_id}) 

    def POST(self): 
     i = web.input() 
     data = web.data() # you can get data use this method 
     print data 
     pass 

if __name__ == "__main__": 
    app.run() 

Posso inviare richieste GET ok, tuttavia quando provo a inviare una richiesta POST, ottengo un errore interno. Al momento, non sono sicuro che l'errore sia dovuto a cURL che non invia il POST correttamente (altamente improbabile) o se il mio server non è implementato correttamente (più probabilmente).

Questo è il comando che uso per inviare la richiesta POST:

curl -i -H "Accept: application/json" -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","active":true http://localhost:8080/xx/xxx/xxxx 

Qui è la risposta del server:

[email protected]:~curl -i -H "Accept: application/json" -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","active":true http://localhost:8080/xx/xxx/xxxx 
HTTP/1.1 500 Internal Server Error 
Content-Length: 1382 
Content-Type: text/plain 

Traceback (most recent call last): 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 1245, in communicate 
    req.respond() 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 775, in respond 
    self.server.gateway(self).respond() 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 2018, in respond 
    response = self.req.server.wsgi_app(self.env, self.start_response) 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/httpserver.py", line 270, in __call__ 
    return self.app(environ, xstart_response) 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/httpserver.py", line 238, in __call__ 
    return self.app(environ, start_response) 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 277, in wsgi 
    result = self.handle_with_processors() 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 247, in handle_with_processors 
    return process(self.processors) 
    File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 244, in process 
    raise self.internalerror() 
TypeError: exceptions must be old-style classes or derived from BaseException, not str 

Qual è la causa dell'errore - e come può risolvere il problema ?

+0

Il messaggio è abbastanza chiaro sulla causa. Denomina il file e il numero di riga. File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", riga 244. È un errore nel codice che non hai scritto. Devi parlare con la gente che mantiene web.py. Non noi. –

+0

Inoltre. Perché non c'è 'return' nel metodo POST? –

+1

Penso che dovresti restituire 'web.notfound (json.dumps ({'ok': 0, 'errcode': 404}))' nella tua funzione not found. È necessaria una modifica simile per la funzione 'internalerror'. –

risposta

4

Ci sono alcuni problemi qui.

1) POST takes 2 arguments (like GET), self and the resource (method_id is fine) 
2) When you're making a POST request you're setting "Content-Type" and not "Accept" 
3) Your JSON isn't in quotes as a string 

Se si cambia il POST (auto, method_id) il seguente dovrebbe funzionare:

curl -i -H "Content-Type: application/json" -X POST -d '{"value":"30","type":"Tip 3","targetModule":"Target 3","active":true}' http://127.0.0.1:8080 

Si dovrebbe anche avvolgere il blocco in un try/tranne per catturare gli errori e fare qualcosa di utile con loro :

def POST(self,method_id): 
    try: 
     i = web.input() 
     data = web.data() # you can get data use this method 
     return 
    except Error(e): 
     print e 
+0

Grazie Kirsten !. A proposito, ho anche dovuto correggere il mio metodo POST per restituire un valore, come suggeriva S. Lott. Per inciso, potresti indicarmi la documentazione sul modo corretto di implementare il metodo POST ?. I documenti sul web.py hanno un esempio in cui l'unico argomento per il metodo POST è self - è da lì che ho preso quell'uso. –

+0

http://johnpaulett.com/2008/09/20/getting-restful-with-webpy/ –