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.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 22, 2015 at 6:14

1 Answer 1

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 
}); 
answered Nov 22, 2015 at 16:20

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.