POST to CartoDB Maps API via NodeJS

المشرف العام

Administrator
طاقم الإدارة
We are attempting to port a Python-based CartoDB authentication sample app to NodeJS:The example, "Basic auth middleware with CartoDB," was created by CartoDB labs.

We have sucessfully replaced one section of the Python code which authenticates to cartodb and makes SQl API calls by utilizing the "cartodb-nodejs" client module.

However, we are stuck on another section which executes a PUT to CartoDB Maps API endpoint, in order to create a Named Map (we have an on-premise Cartodb instance, so we utilize a custom endpoint URL).

What we need to do is to POST a JSON-formatted template (stored in the "named_map" variable, below) to our on-premise endpoint.

var named_map ={ "version": "0.0.1", "name": "testmap", "auth": { "method": "token", "valid_tokens": ["password"] }, "placeholders": { "color": { "type": "css_color", "default": "red" }, "filter": { "type": "number", "default": 1 } }, "layergroup": { "version": "1.0.1", "layers": [ { "type": "cartodb", "options": { "cartocss_version": "2.1.1", "cartocss": "#layer { polygon-fill: ; }", "sql": "select * from v1_8_walgreens" } } ] }}We can accomplish the equivalent task via cURL and a stored/file-based template, "template.json".

curl -X POST -H "Content-Type: application/json" -d @template.json "https://cartodb.brighterdevelopment..._key=fd8bd0a4c99c82af698142585ea8ae71a52421a5"

Below is the actual Python code section we need to duplicate in Node. NOTE: the endpoint, api_key and named map name are basically passed-in as variables/configs.

requests.put(os.path.join(config.get('cartodb', 'maps_endpoint'), "named", map_name), data=json.dumps(named_map), params={"api_key": config.get('cartodb', 'api_key')}, headers={'content-type': 'application/json'})

We have made a few unsuccessful attempts at implementing this with the Node “Request” Package.We are open to any approach.Many thanks!



أكثر...
 
أعلى