I am using Dart Shelf framework for building an API. Get works fine but I am having issues with post. I couldn't access any of the body parameters of the post request in my server. Here is what I have tried.
// shelf-router
router.post('/login', (Request req) async {
final body = await req.readAsString();
// to check
print(body); // outputs null
return
Response.ok('OK');
});
How am I testing this?
Using postman with the endpoint and body type raw (JSON).
payload as
{
"user":"testUser",
"password":"p455w0rd"
}
I even tried setting contenttype header to application/JSON but no luck there too.
Try this inside your request handler function..
final String query = await request.readAsString();
Map queryParams = Uri(query: query).queryParameters;
print(queryParams);
final String query = await request.readAsString();
// Map<String, String> queryParams = Uri(query: query).queryParameters;
Map queryParams = jsonDecode(query);
print(queryParams['user']);
I can receive the parameter from the postman with form-data.
I am using shelf_route in the server.
If this is similar to you, you can follow this:
https://stackoverflow.com/a/74255231/17798537
I'm trying to use Twitter API to get all tweets in NYC. Now I want to store all these tweets to my local json file(data.json). But after running this code, I found only the last tweet get stored not all of them.
var Twitter = require('twitter');
const fs = require('fs');
var client = new Twitter({
consumer_key: 'XXXXXXXXXXXX',
consumer_secret: 'XXXXXXXXXXXX',
access_token_key: 'XXXXXXXXXXXX',
access_token_secret: 'XXXXXXXXXXXX'
});
client.stream('statuses/filter', {locations: '-74,40,-73,41'}, function(stream) {
stream.on('data', function(tweet) {
fs.writeFile('./data.json', JSON.stringify(tweet), 'utf-8');
console.log(tweet.text);
});
stream.on('error', function(error) {
throw error;
});
});
How to store all of them in data.json? It's kind of like what '+=' operation does but I'm new to js...
It sounds like you need to open your file first, parse the JSON, append your new result to the array, transform it back into a string and save it again.
I believe this is what you are looking for here: https://stackoverflow.com/a/36093141/5884189
I'm looking for a way to access the JSON being sent back to the requestor in the "after" filter for a controller.
var locomotive = require('locomotive');
var myController = new locomotive.Controller();
myController.after('myAction', function(next) {
var response = {}; //I want to access the JSON being sent back in myAction: {'hello':'world'}
console.log(response); //this should log "{'hello':'world'}"
next();
});
myController.myAction = function myAction() {
this.res.json({'hello':'world'});
}
module.exports = myController;
If anyone has any way of doing this, it would be much appreciated.
In your main action, assign your json to an object on this (res is reserved):
myController.myAction = function myAction() {
this.model = {'hello':'world'};
this.res.json(this.model);
}
Then you can access it in your after filter:
myController.after('myAction', function(next) {
var model = this.model;
console.log(model);
next();
});
I found a "hack" solution... It's not the cleanest, and requires changing the code within the express response.js file in "node_modules"...
If anyone has a better option where you can access the json being sent in response to the request within the controller action (or controller filter) itself, I'd greatly appreciate it.
Thanks.
in the ~/node_modules/locomotive/node_modules/express/lib/response.js file, I altered the "res.json" function (line 174 for me) to include the following line after the declaration of the body variable (which is passed to the send function).
this.responseJSON = body;
This allows you to access this.responseJSON within a controller's after filter, as follows:
myController.after('myAction', function(next) {
**var response = this.res.responseJSON; //ACCESS RESPONSE JSON HERE!!!!!**
console.log(response); //Now logs "{'hello':'world'}"
next();
});
Like I said, not the most elegant, but gets the job done in a pinch. Any more elegant solutions welcome...
I am using OpenLayers and geoserver to build an app. very new to everything and this is my first app. Now I am trying to use the WMS getFeatureInfo to fetch feature information and show in popups when users click on a feature. To get around the cross domain issue, I am now trying to get JSONP response. the response i got is:
parseResponse({"type":"FeatureCollection","features":[{"type":"Feature","id":"Fire_Boundary_Pro.1","geometry":{"type":"MultiPolygon","coordinates":[[[[414495.86280000024,6451439.778],[414501.3269999996,6451437.0124],[414572.5887000002,6451444.5907],[414614.8359000003,6451368.1611],[414679.86149999965,6451410.5882],[414793.0769999996,6451376.6343],[414905.6501000002,6451419.4576],[414983.7874999996,6451315.405],[414978.77660000045,6451203.6776],[415021.0197999999,6451127.2464],[415051.8420000002,6450994.8769],[415029.2346000001,6450855.0812],[414899.8300999999,6450693.4524],[414882.8183000004,6450595.5852],[414776.48950000014,6450517.9117],[414747.5351999998,6450426.9246],[414688.4584999997,6450384.5476],[414605.3772,6450369.8903],[414568.95940000005,6450460.3295],[414555.8437000001,6450606.8071],[414473.11259999964,6450550.2695],[414468.34250000026,6450410.6221],[414433.15529999975,6450354.4835],[414350.7204999998,6450263.0455],[414273.40699999966,6450269.3751],[414076.47389999963,6450365.4401],[414061.89190000016,6450388.7117],[414037.87590000033,6450380.4262],[413891.39940000046,6450430.6506],[413934.48699999973,6450516.7853],[413948.07650000043,6450636.9786],[413961.37650000025,6450791.4776],[414092.2400000002,6450861.1987],[414153.67080000043,6450897.9731],[414179.43510000035,6450913.3962],[414281.23610000033,6450965.7158],[414279.7922,6451137.244],[414352.3854,6451189.3169],[414395.91280000005,6451223.991],[414350.94269999955,6451360.8451],[414495.86280000024,6451439.778]]]]},"geometry_name":"the_geom","properties":{"area":8.09003398112E-5,"Shape_Leng":4319.38797802,"Shape_Area":828429.079784}}]})
However I am not sure how to parse the JSONP response and obtain a property value. I am trying to use the OpenLayers.Format.JSON.read method (not sure if this is the right way to do it) but it returns an error that it is an undefined constructor. Here is my code:
map.events.register('click', map, function (e) {
document.getElementById('nodelist').innerHTML = "Loading... please wait...";
var params = {
REQUEST: "GetFeatureInfo",
EXCEPTIONS: "text/javascript",
BBOX: map.getExtent().toBBOX(),
SERVICE: "WMS",
//use JSONP format
INFO_FORMAT: 'text/javascript',
QUERY_LAYERS: map.layers[0].params.LAYERS,
FEATURE_COUNT: 50,
Layers: 'Bushfire_Com_Study:Fire_Boundary_Pro',
WIDTH: map.size.w,
HEIGHT: map.size.h,
format: format,
styles: map.layers[0].params.STYLES,
srs: map.layers[0].params.SRS,
// handle the wms 1.3 vs wms 1.1 madness
if(map.layers[0].params.VERSION == "1.3.0") {
params.version = "1.3.0";
params.j = parseInt(e.xy.x);
params.i = parseInt(e.xy.y);
} else {
params.version = "1.1.1";
params.x = parseInt(e.xy.x);
params.y = parseInt(e.xy.y);
}
// merge filters
if(map.layers[0].params.CQL_FILTER != null) {
params.cql_filter = map.layers[0].params.CQL_FILTER;
}
if(map.layers[0].params.FILTER != null) {
params.filter = map.layers[0].params.FILTER;
}
if(map.layers[0].params.FEATUREID) {
params.featureid = map.layers[0].params.FEATUREID;
}
OpenLayers.loadURL("http://localhost:8080/geoserver/Bushfire_Com_Study/wms", params, this, setHTML, setHTML);
OpenLayers.Event.stop(e);
});
// sets the HTML provided into the nodelist element
function setHTML(response){
var json_format = new OpenLayers.Format.JSON();
var object = json_format.read(response);
document.getElementById('nodelist').innerHTML = object.features[0].properties['area'];
};
An old question but I couldn't find an answer anywhere else. Most important sources for a solution were
http://dev.openlayers.org/docs/files/OpenLayers/Protocol/Script-js.html
and
http://docs.geoserver.org/stable/en/user/services/wms/vendor.html#wms-vendor-parameters.
My code contained things similar to the following.
// The Script protocol will insert the JSONP response in to the DOM.
var protocol = new OpenLayers.Protocol.Script({
url: someUrl,
callback: someCallbackFunction,
});
// GeoServer specific settings for the JSONP request.
protocol.callbackKey = 'format_options';
protocol.callbackPrefix = 'callback:';
// WMS parameters like in the question
var params={
REQUEST: "GetFeatureInfo",
EXCEPTIONS: "text/javascript",
INFO_FORMAT: 'text/javascript',
//etc
};
// Send the request.
protocol.read({
params: params
});
// Callback to handle the response.
function someCallbackFunction(response) {
for(var feature in response.features) {
// Do something with the returned features.
}
}
I am using the Facebook C# SDK in my Silverlight 4 browser app to perform some requests to the Facebook Graph API. I followed the example in the SDK documentation to request the user's information (using the asynchronous method to make it work on Silverlight):
var fb = new FacebookClient(accessToken);
fb.GetAsync("/me");
fb.GetCompleted += (o, ea) =>
{
var result = (JsonObject)ea.GetResultData();
var name = (string)result["name"];
};
This way I get the JsonObject without any problem and I can read all data, but when I make request to "me/feed" or "me/home" instead of "/me":
fb.GetAsync("/me/home");
fb.GetCompleted += (o, ea) =>
{
var result = (JsonObject)ea.GetResultData();
var data = (JsonArray) result["data"];
foreach (JsonObject post in data)
{
id = (string)post["id"];
}
};
then the JsonObject is empty and I get a exception when trying to access its elements. I successfully managed to POST a message to "me/feed", but why do I receive an empty response when making a GET request? I have set the access token in the FacebookClient I'm using to make the calls, is there something else that I'm missing?
Thanks!