2013-04-22 99 views
1
APIURL ='http://localhost:6543/api/patches/alice_8b84090712bce46e15a8107839cefe/e5678' 
data = { 
    'patch_id' : 'e5678', 
    'queue_id' : 'alice_8b84090712bce46e15a8107839cefe', 
} 
response = requests.get(APIURL, data=data) 

我有上面的代碼來測試檐口金字塔REST api。金字塔讀取http數據

但是,我無法讀取通過參數data=data輸入的數據。

這是此端點服務器功能:

@resource(collection_path='/api/patches', path="/api/patches/{queue_id}/{patch_id}") 
class Patch(object): 
    def __init__(self, request): 
     self.request = request 

    def get(self): 
     """ 
     """ 
     queue_id = self.request.matchdict.get('queue_id', '') 
     patch_id = self.request.matchdict.get('patch_id', '') 
     data = { 
      'queue_id': 'e12525e1f90ad5e7395a965', 
      'patch_id': 'a9651a8259a666c0032f343', 
      'node_id': 'bef3a2adc76415b2be0f6942b5111f6c5e5b7002', 
      'message': 'This is a patch on feature2.', 
      'datetime': '.....', 
     } 

     #TODO call the patch method to get the public attributes 

     return {'error':{}, 'data': data} 

回答

5

請求被忽略了data參數設置爲that parameter is used to provide the body of the response的數據(和GET不具有體)。如果你是罰款通過查詢字符串參數 - 那就是:

http://localhost:6543/api/patches?queue_id=12345&patch_id=910 

,那麼你可以使用params關鍵字參數,而不是:

requests.get(APIURL, params=data) 

否則,您可以使用urljoinurlparse在構建您的網址標準庫:

APIURL = "http://localhost:6543/api/patches" 
with_queue = urljoin(APIURL, queue_id) 
with_patch = urljoin(with_queue, patch_id) 
response = requests.get(with_patch)