2012-01-11 26 views
5

Tôi đang trong quá trình viết một bằng chứng của khái niệm máy chủ RESTful sử dụng web.pyBằng chứng về khái niệm RESTful Python máy chủ (sử dụng web.py) + thử nghiệm với cURL

Đây là kịch bản:

#!/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() 

Tôi có thể gửi yêu cầu GET, tuy nhiên khi tôi cố gửi yêu cầu POST, tôi gặp lỗi nội bộ. Hiện tại, tôi không chắc liệu lỗi đó có phải do cURL không gửi POST đúng cách (không chắc) hay máy chủ của tôi không được triển khai chính xác (nhiều khả năng).

Đây là lệnh tôi sử dụng để gửi yêu cầu 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 

Dưới đây là câu trả lời 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 

nguyên nhân của lỗi này là gì - và làm thế nào tôi có thể sửa chữa nó ?

+0

Thư khá rõ ràng về nguyên nhân. Nó đặt tên tệp và số dòng. Tệp "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", dòng 244. Đó là lỗi trong mã bạn không viết. Bạn cần nói chuyện với những người duy trì web.py. Không phải chúng tôi. –

+0

Ngoài ra. Tại sao không có 'return' trong phương thức POST? –

+1

Tôi nghĩ bạn nên trả về 'web.notfound (json.dumps ({'ok': 0, 'errcode': 404}))' trong hàm notfound của bạn. Thay đổi tương tự là bắt buộc đối với hàm 'internalerror'. –

Trả lời

4

Có một số vấn đề ở đây.

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 

Nếu bạn thay đổi POST của bạn để (tự, method_id) sau đây nên làm việc:

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 

Bạn cũng nên quấn khối trong một thử/trừ để bắt lỗi và làm điều gì đó hữu ích với họ :

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

Cảm ơn Kirsten !. BTW, tôi cũng phải sửa phương thức POST của mình để trả về một giá trị - như S. Lott đã đề xuất. Ngẫu nhiên, bạn có thể vui lòng chỉ cho tôi tài liệu về cách chính xác để triển khai phương thức POST không ?. Các tài liệu trên web.py có một ví dụ trong đó chỉ arg cho phương thức POST là tự - đó là nơi tôi đã sử dụng từ đó. –

+0

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

Các vấn đề liên quan