JSON.parse() is not working in Worklight Hybrid Adapter - adapter

I am trying to use Adapter for Push Notification. I followed all steps define in this IBM Worklight Developer site for Push Notification and make a project and its working. Now further i want to send notifications to a specific device. For this worklight give a method getDeviceSubscriptions() which return JSON array containing number of object for each subscribed device.
deviceSubscriptions = userSubscription.getDeviceSubscriptions() ;
the JSON include:
[{
"platform":"Google",
"eventSourceId":"PushAdapter.PushEventSource",
"alias":"myPush",
"token":" ",
"userAgent":" ",
"device":" ",
"applicationId":" ",
"options":{}
}]
Now i want to use the data in JSON for getting device and token. Now for this i use JSON.stringify(deviceSubscriptions) this method convert deviceSubscriptions into string. But i want that i can direct access to token and device in JSON array for this i used JSON.parse(deviceSubscriptions ) but this giving error:
"Ecma Error: TypeError: Cannot find default value for object. (C%3A%5Cworkspace%5CFINAL%5Cadapters%5CPush/Push-impl.js#43)"
Line 43 is:
JSON.parse(deviceSubscriptions)
Any help would be appreciated

JavaScript code for the Worklight Adapters runs on the Worklight Server using Mozilla Rhino. Rhino is an open-source implementation of JavaScript written entirely in Java. You may not have access to everything the JavaScript engines running on the clients have access to.
Seems like either JSON or JSON.parse is undefined. Try adding the following library to your Worklight Adapter [name]-impl.js file:
json2.js: This file creates a JSON property in the global object, if there
isn't already one, setting its value to an object containing a stringify
method and a parse method. The parse method uses the eval method to do the
parsing, guarding it with several regular expressions to defend against
accidental code execution hazards. On current browsers, this file does nothing,
prefering the built-in JSON object.
Source.

Related

How to pass dynamic value to JSON as a API request in flutter flow?

I have two page
Login/Registration Page as of now
OTP Verification Page
API has been created and in api, we are passing phone number as request and gets otp as response. However i would like your help to know how should i able to pass the phoneNumber textfield data to APIcall json request in flutterflow?
If there is anyway or any documentation will be really helpful?
I have tried with variable method, however i was not able to pass it to json.
I have also tried jsonpath method, but no luck.
Define dynamic part of API url with brackets
https://jsonplaceholder.typicode.com/posts/[postId]
https://jsonplaceholder.typicode.com/comments?postId=[postId]
then create the variable (postId) with the same name. When adding the API call, it is required to add these variables.
See API Calls 101 for details.

Accept/Content-Type header based processing in Quart and Quart-Schema

Because I am rewriting a legacy app, I cannot change what the clients either send or accept. I have to accept and return JSON, HTML, and an in-house XML-like serialization.
They do, fortunately set headers that describe what they are sending and what they accept.
So right now, what I do is have a decoder module and an encoder module with methods that are basically if/elif/else chains. When a route is ready to process/return something, I call the decoder/encoder module with the python object and the header field, which returns the formatted object as a string and the route processes the result or returns Response().
I am wondering if there is a more Quart native way of doing this.
I'm also trying to figure out how to make this work with Quart-Schema. I see from the docs that one can do app.json_encoder = <class> and I suppose I could sub in a different processor there, but it seems application global, there's no way to set it based on what the client sends. Optimally, it would be great if I could just pass the results of a dynamically chosen parser to Quart-Schema and let it do it's thing on python objects.
Thoughts and suggestions welcome. Thanks!
You can write your own decorator like the quart-schema #validation_headers(). Inside the decorator, check the header for the Content-Type, parse it, and pass the parsed object to the func(...).

How can I send a request to api.myjson.com with Swift and Xcode?

I am trying to update a JSON file from my iOS app in Swift. I do not want to write a separate JQuery script, since I do not have enough knowledge nor the time to do so. The JSON file is hosted at the myjson api at api.myjson.com Here they explain how to use the api: http://myjson.com/api
I understand that each method such as 'PUT' or 'POST' is appended to the end of the website, like api.myjson.com/example?type=PUT.
What I am trying to figure out is the syntax to add to or update my JSON files stored here.
The url in .ajax() calls follows the exact same rules as an href or src in plain html would:
e.g you're on http://example.com/dir/subdir/somescript.php:
$.ajax('foo.php'); -> http://example.com/dir/subdir/foo.php
$.ajax('/foo.php); -> http://example.com/foo.php
$.ajax('../foo.php'); -> http://example.com/dir/foo.php
The method used is generally GET, but you can specify whatever method you want in the options:
$.ajax(url, {method:"POST"});

How do I route websocket requests to SailsJS without breaking the built in abstraction

I am new to SailsJS and I am building an API for a iOS application. As per the SailsJS documentation it provides and abstraction for the Socket IO requests and routes it to my controllers.
For example if I emit some data to /user/create it will automatically call the UserController -> create function. From what I understood I need to use the sails.io.js file on client side and emit using Socket.get and Socket.post.
As I am using this as an API for an iOS where iOS app is the client, how do I emit data to the server while using the routing feature in Sails JS ?
If you look at the source code for sails.io.js, you'll see it's just a bunch of helper methods thinly wrapping the Socket.io client. The upshot is, you can still use Sails' wonderful routing-over-sockets mechanism using regular socket.emit calls. Just use the HTTP method as the event name, and send the URL and data as the payload. For example:
socket.emit('get', {url: 'http://example.com/foo'}, cb);
socket.emit('post', {url: 'http://example.com/bar', data: {name: 'joe'}}, cb);
where cb is a callback that will receive the result of the call as its sole argument.
Upcoming versions of Sails will support a second argument to the callback which contains more data about the response, like a status code and errors.

ASP.NET WEB API 406 error:for POST request using Media format

I am very new to web api stuff:
I am getting an error
406: Not Acceptable
error message in asp.net web api rest service.
In my rest service I’m using media format for my customized XML output, to get customized output.
I’m registering my formatted media in Global.asax page.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new mynewformat());
all my methods are post methods with typed object as parameter and parameters are accepts from body.
Whenever I try to test the service… Getting 406: Not acceptable error message.
can anyone please help me ... what could be the reason for this....???
I did notice couple of interesting points here...
If I’m commenting below line then I’m getting 200 (OK) status code (which is fine.)... but format is not applying to output.
GlobalConfiguration.Configuration.Formatters.Clear();
If i'm removing parameters in my service method.. Then its working
fine..
I request everyone.. Please guide me what could be the reason/work around/solution/fix..for this issue.
Note:I don't want accept parameters from URI so i made it to accept from frombody only.
Thanks.
There is a lot more to implementing a custom format than just adding it to the configuration formatters. It starts with having to change the media-type header to a new custom type of your choosing (like "application/myNewFormat") for all requests, for the client. On the back end, you have to implement a new MediaTypeFormatter that can handle the serialization. This involves a bit more of code.
A good example of this resides here, it can easily be stripped to boiler-plate code:
http://www.codeproject.com/Articles/559378/Implementing-Custom-Media-Formatters-in-ASP-NET-We

Resources