pythonrestpyramidcornice

Pyramid read http data


APIURL ='http://localhost:6543/api/patches/alice_8b84090712bce46e15a8107839cefe/e5678'
data = {
    'patch_id' : 'e5678',
    'queue_id' : 'alice_8b84090712bce46e15a8107839cefe',
}
response = requests.get(APIURL, data=data)

I have the above code to test a cornice pyramid REST api.

However I am having trouble reading data entered through the data=data parameter.

This is the server function for this endpoint:

@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}

Solution

  • Requests is ignoring the data provided in the data parameter as that parameter is used to provide the body of the response (and GET doesn't have a body). If you are fine with passing query-string arguments - that is:

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

    then you can use the params keyword argument instead:

    requests.get(APIURL, params=data)
    

    Otherwise, you can construct your URL using urljoin from urlparse in the standard library:

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