Cha*_*ngh 2 python rest pyramid cornice
APIURL ='http://localhost:6543/api/patches/alice_8b84090712bce46e15a8107839cefe/e5678'
data = {
'patch_id' : 'e5678',
'queue_id' : 'alice_8b84090712bce46e15a8107839cefe',
}
response = requests.get(APIURL, data=data)
Run Code Online (Sandbox Code Playgroud)
我有上面的代码来测试一个檐口金字塔REST api.
但是,我无法读取通过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}
Run Code Online (Sandbox Code Playgroud)
请求忽略data
参数中提供的数据,因为该参数用于提供响应的主体(并且GET
没有主体).如果您对传递查询字符串参数没问题 - 即:
http://localhost:6543/api/patches?queue_id=12345&patch_id=910
Run Code Online (Sandbox Code Playgroud)
那么您可以使用params
关键字参数:
requests.get(APIURL, params=data)
Run Code Online (Sandbox Code Playgroud)
否则,您可以使用标准库中的urljoin
from 构建URL urlparse
:
APIURL = "http://localhost:6543/api/patches"
with_queue = urljoin(APIURL, queue_id)
with_patch = urljoin(with_queue, patch_id)
response = requests.get(with_patch)
Run Code Online (Sandbox Code Playgroud)