从Web.py中的PUT方法获取输入

时间:2011-09-30 00:11:21

标签: http web.py put

我在服务器程序中使用以下代码:

class AddLibSong:
  def PUT(self):
  db = MahData.getDBConnection()
  songs = json.loads(web.input().to_add)
  addToLibrary(songs)
  return

但出于某些原因,当我使用数据进行PUT时:

"to_add=[ { "album" : "Unknonwn", "artist" : "Unknonwn", "host_lib_id" : "1", "is_deleted" :
"false", "server_lib_id" : "-1", "song" : "Moneytalks" } ]" 

我收到以下错误:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/application.py", line 237, in process
    return self.handle()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/application.py", line 228, in handle
    return self._delegate(fn, self.fvars, args)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/application.py", line 409, in _delegate
    return handle_class(cls)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/application.py", line 385, in handle_class
    return tocall(*args)
  File "/Users/kurtis/sandbox/udj/webserver/Library.py", line 114, in PUT
    song = json.loads(web.input().to_add)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/web/utils.py", line 76, in __getattr__
    raise AttributeError, k
AttributeError: 'to_add'

127.0.0.1:51096 - - [29/Sep/2011 19:02:58] "HTTP/1.1 PUT /add_songs_to_library" - 500 Internal Server Error

有人知道这是为什么吗?我想我看到一些关于Web.py的东西,只有在给出POST或GET时才能获得输入,但我没有在源代码中看到任何应该阻止它的内容。

1 个答案:

答案 0 :(得分:1)

无论如何,如果你想了解如何在WebPy上使用PUT的更多细节,我会建议你this很棒的链接。

要使其在最新版本的webpy上运行,您应该将“主要”代码更改为:

if __name__ == "__main__":

    app=web.application(urls, globals())
    app.run()