2

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: <%= color %>; }",
                    "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.com/user/demo-admin/api/v1/map/named?api_key=API_KEY_GOES_HERE"

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.

1

I found a simple solution utilizing the "request-json" package:

var request = require('request-json');  
var client =  request.createClient('https://cartodb.brighterdevelopment.com/user/demo-admin/api/v1/map/named?api_key=api_key_here');  
client.post('', named_map, function(err,body) {  
    //do something  
});  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.