RESTful Java GET method - json

Could I send a request GET to a restful server,that contain a JSON object ?
In case that I can, how can I get it from server side ?
Thanks in advance

Most restful servers working with data in JSON format (if not in parameter, or XML format). You can take a look at the example at the jQuery page. You can also call publicly available services such as Google weather service.
Example of AJAX call with using the JSON data:
var data = { name: "John", time: "2pm" };
$.getJSON( "test.js", data )
.done(function( json ) {
console.log( "JSON Data: " + json.users[1].name );
})

Related

Parsing Contentful Webhooks (Custom JSON types)

I'm currently using this Contentful-webhook-server to listen for webhooks when content is unpublished.
server.listen(30000, function(){
console.log('Contentful webhook server running on port ' + 30000)
});
server.on('ContentManagement.Entry.publish', function(req){
console.log('An entry was published!', req);
});
server.on('ContentManagement.Entry.unpublish', function(req){
console.log('An entry was unpublished!');
console.log('Deleted SOLR ID: ', req);
});
I'm trying to parse the response I've got but I can't seem to find a way to parse the custom JSON they use in their response. Should I be creating my own server with express or am I missing a way to get the response body in this example code.
The contentful-webhook-server library uses the plain node http module for the server. Thus, the req object is a readable stream that you need to buffer and parse to get the body.
Take a look at https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/#request-body for an example.

How to get indented JSON output in asp.net MVC 5 to view in browser?

This is only for debugging purpose. Only returning JSON, outputs unreadable JSON string on webpage. Is there any quick solution to view formatted json on webpage?
Looking for a C# function that will take json object and return formatted output as string from my action method.
Just change JSON global settings in your Global.asax file.
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented
};
It should produce indented JSON for all your Web API endpoints.
Then you can inspect resulting JSON directly in browser or using some HTTP traffic capturing tool like Fiddler.
Use the JavaScript JSON.Stringify() function. Example:
$.ajax({
method: "POST",
url: "/yourController/yourAction",
data: { name: "John", location: "Boston" }
}).done(function( data ) {
alert(JSON.Stringify(data));
});

How to update json file in AngularJS

I want to update the existing json file in AngularJS
JSON file:
{
"text1": "Click here to edit!",
"text2": "Click here to edit!",
"text3": "Click here to edit!",
"text4": "Click here to edit!"
}
I want to update this JSON file as:
text1: "Abc"
and save this changes in JSON file
You can not update a json file without using a server-side language like PHP or python. Basically it is security compliance. For more understanding kindly go through
https://docs.angularjs.org/guide/security
https://docs.angularjs.org/api/ng/directive/ngCsp
and
https://developer.mozilla.org/en-US/docs/Web/Security/CSP
Imagine you have getjson.php and savejson.php in the server which work exactly as their names suggest.
Now use $http service of Angular to retrieve your json from the server.
$http.get("getjson.php").then(function(response){
$scope.myJsonObject = response.data;
//Your json becomes JS object here. Change it the way you want
$scope.myJsonObject.text1 = "Abc";
});
Use $http service again to send your json back to the server.
$http({
method: "post",
url: "savejson.php",
data: $scope.myJsonObject,
headers: { 'Content-Type': 'application/json; charset=utf-8' }
});
This is the basic. Please note that you need to do your php part to save/load your json file. Also you should handle errors of the $http service.
Please see how $http service and promises work.

Retrieve JSON Payload in Server Response from a Post Request with Ember Data

I must be really missing something but I can't find a way to retrieve the json payload from a post request using ember data. For example:
var onSuccess = function(data) {
//Want to get json payload from data
};
var onFail = function(data) {
//Want to get json payload from data
};
// save model which sends a post request to the server
model.save().then(onSuccess, onFail);
Any help much appreciated!
Ember Data automatically processes the response from a POST and inserts and/or replaces any objects with matching IDs. Objects are extracted from the payload automatically and placed in the store. Read about Ember Data's REST adapter and the conventions expected of your server

PUT requests with Custom Ember-Data REST Adapter

I'm using Ember-Data 1.0.0.Beta-9 and Ember 1.7 to consume a REST API via DreamFactory's REST Platform. (http://www.dreamfactory.com).
I've had to extend the RESTAdapter in order to use DF and I've been able to implement GET and POST requests with no problems. I am now trying to implement model.save() (PUT) requests and am having a serious hiccup.
Calling model.save() sends the PUT request with the correct data to my API endpoint and I get a 200 OK response with a JSON response of { "id": "1" } which is what is supposed to happen. However when I try to access the updated record all of the properties are empty except for ID and the record on the server is not updated. I can take the same JSON string passed in the request, paste it into the DreamFactory Swagger API Docs and it works no problem - response is good and the record is updated on the DB.
I've created a JSBin to show all of the code at http://emberjs.jsbin.com/nagoga/1/edit
Unfortunately I can't have a live example as the servers in question are locked down to only accept requests from our company's public IP range.
DreamFactory provides a live demo of the API in question at
https://dsp-sandman1.cloud.dreamfactory.com/swagger/#!/db/replaceRecordsByIds
OK in the end I discovered that you can customize the DreamFactory response by adding a ?fields=* param to the end of the PUT request. I monkey-patched that into my updateRecord method using the following:
updateRecord: function(store, type, record) {
var data = {};
var serializer = store.serializerFor(type.typeKey);
serializer.serializeIntoHash(data, type, record);
var adapter = this;
return new Ember.RSVP.Promise(function(resolve, reject) {
// hack to make DSP send back the full object
adapter.ajax(adapter.buildURL(type.typeKey) + '?fields=*', "PUT", { data: data }).then(function(json){
// if the request is a success we'll return the same data we passed in
resolve(json);
}, function(reason){
reject(reason.responseJSON);
});
});
}
And poof we haz updates!
DreamFactory has support for tacking several params onto the end of the requests to fully customize the response - at some point I will look to implement this correctly but for the time being I can move forward with my project. Yay!
EmberData is interpreting the response from the server as an empty object with an id of "1" an no other properties in it. You need to return the entire new object back from the server with the changes reflected.