'How to send query from nodejs server to open data server?
I'm working with open data: https://data.sfgov.org/Economy-and-Community/Mobile-Food-Facility-Permit/rqzj-sfat
The open data site allows to query the data, and example query is below: https://soda.demo.socrata.com/resource/4tka-6guv.json?$where=magnitude > 3.0 However, this, I imagine is URL querying. If I had to get the same object on my nodejs server, how would I format the query to receive the json objects on the node server?
Are there any security threats or exposing threats to querying for data this way?
Solution 1:[1]
Probably the easiest way to do this is via request package. You could do something like the following:
var request = require('request');
request('https://soda.demo.socrata.com/resource/4tka-6guv.json?$where=magnitude%3E3.0', function (err, res, body) {
if (err) {
//handle error
} else {
console.log(body); // and do stuff with your results
}
});
If you run this, you'll see a print out of your result array. There aren't really any security threats with this. Your just doing a GET request from an open data source. Pretty standard stuff. Same kind of transactions happen everyday all over the web.
Solution 2:[2]
Check out the request module. Super useful
Solution 3:[3]
You can use the soda-js library.
Here is a MWE: just issue an npm install soda-js and then you can do something similar to:
const soda = require('soda-js');
const consumer = new soda.Consumer('datos.gov.co');
consumer.query()
.withDataset('sus6-4i9j')
.limit(5)
.getRows()
.on('success', function(rows) { console.log(rows); })
.on('error', function(error) { console.error(error); });
In this example the endpoint being consumed is https://www.datos.gov.co/resource/sus6-4i9j.json, which returns a list of other APIs openly available in the www.datos.gov.co site ;).
The following is a portion of the output:
In your specific case, you can do
const soda = require('soda-js');
const consumer = new soda.Consumer('data.sfgov.org');
consumer.query()
.withDataset('rqzj-sfat')
.limit(5)
.getRows()
.on('success', function(rows) { console.log(rows); })
.on('error', function(error) { console.error(error); });
Which will provide the data you need.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Hayden Braxton |
| Solution 2 | Joey Wood |
| Solution 3 |

