As far as I understand, for AJAX generally js/jquery are used, so if I plan some AJAX actions for action, I should add format.js {...}.
I googled this problem and as far as i understand, main JSON use is for creating application's API.
Any other reasons to include format.json and json rendering in the application?
I think this is a common misunderstanding.
Sending http request through Ajax has nothing to do with rendering HTTP response in a specific format. Writing in the controller action fromat.js{.....} or format.html{...} has nothing to do with AJAX. it is just you are telling your controller action to respond based on the request format. regardless to how the request send.
When you send AJAX request you can determine the expected data type or content-type by using the following $.ajax attribute dataType: "json".
For creating APIs as far as I know most of apis if not all of them are using either xml or json. the latter has become very popular. Most of the newly built apis are built to send data in a json format.
So when you built you API you need to design you action response to be in a json format. There are many ways to do such a thing.
My favourite way is to build a json view this can be done using jbuilder which is included by default as a gem when you create a new rails app OR use the my favourite json builder gem which is rabl.
Related
I see that Grails 2.3 is using REST for the CRUD actions in scaffolding. While it's a great way to learn how REST works, I am wondering if using REST to communicate inside of a single application stack is very efficient. Doesn't it send the request all the way up to the network layer and back down again instead of going directly from the app server to the database? I am visualizing a "pop fly" as opposed to a "line drive." Am I misunderstanding how this works?
I assume when you say, "using REST for the CRUD actions in scaffolding" you are referring to the basic scaffolding (i.e. generate-all example.Book). The scaffolded controllers are not calling the REST API (https://yourapp/book.json) to retrieve the data, they are still using GORM to access the database, and then using the Respond method to render the data in the appropriate format based on the Request's content-type (XML, JSON, HTML). So,
Typical request-response cycle
Client, typically an HTML page, makes request (GET http://yourapp/books/1)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to HTML
Grails "responds" to the request with an HTML response using a view from the views directory
SPA/API call
Client, typically javascript, makes request (GET http://yourapp/books/1.json **)
Grails 'parses' request params (id: 1)
Grails' GORM retrieves data from database and creates object instance
Grails resolves response content type to JSON
Grails "responds" to the request with an JSON response
Client consumes the JSON response and acts accordingly
** content-type can be specified in a number of ways, just used the .json suffix since it is the most transparent. See http://grails.org/doc/2.3.x/guide/single.html#contentNegotiation.
And to answer your question regarding whether it "very efficient". I would argue that it is almost always yes, because your payload tends to be much smaller, since you are only transferring data, not data + formatting (HTML, javascript, css, etc). It also provides a way of separating concerns, allowing the client to focus on state and presentation and the backend to focus on data. Furthermore, it means that you can create multiple clients (mobile, desktop-based, web-based) using the same backend API.
In one web service written in Rails, I would like to answer with a file along with additional information.
For this, I consider respond with multipart data. How can I send a multipart response with a file and json?
If there is a better way to do this, please let me know. Note that is not possible add the extra data in the file I'm sending.
Extra points for the face of the problem, that is send a file and data at same time. I already accomplished that by doing a multipart request, but if is there a better way to do this, I would like to know.
I don't know exactly what kind of front end you are using and what your browser compatibility requirements are, or you need the webservice for integration with other apps only, but assuming you are communicating with server over ajax and your app is running in modern browser (or you are allowed to use flash plugin), you can return file contents as base64 encoded string as a part of json response. So in rails controller you would have something like this:
render json: {success: true, file: {name: 'invoice.pdf', data: Base64.encode64(#file.read), extra_info: 'some info'}}
on client side you can process that json, get all the metadata you need from it and also let user save the file to their computer. For that you can use flash plugin or native api implementation
This way you can return couple files with any metadata you want with them to user and on client side user can save files if needed. Also, same webservice can be consumed by other applications as long as they can parse json.
try using gem 'curb' or 'rest-client' gem.
https://github.com/rest-client/rest-client
and
https://github.com/taf2/curb
I'm sure you have done some googling already, have you seen this already? It seems like there is a Gem for what you are trying to accomplish, the Gem however seems to be pretty old.
I have zero experience with Web API and JSON. My requirements are to create a service (I'm using Web API for this) that will accept MIME Encoded JSON data. The service will take that data, insert it into a database, and return the primary key value back to the client.
My hang-up is being able to know where to even start with this. A couple of questions that I have are:
When the device sends the JSON data, how will the service "accept" it? Meaning, what's being passed to the service isn't an URL that we commonly see with MVC (/Controller/Action/ID) which then invokes the Action Method. So, how will the service know what to invoke if I'm passing raw JSON data?
How would I test this if I don't have a device that sends the JSON data yet? Would I manually invoke an AJAX call and call that particular action method and pass in the JSON data that way?
I apologize for the seemingly elementary questions.
Thanks.
When you call a WebAPI-method you still have to specify the endpoint:
Example:
PUT /api/people
MVC knows from that that it should call the put-method on the PeopleController.
You can send raw JSON-data to test it. A good tool for that is HttpFiddler: http://fiddler2.com/
As for where to start, try to create a basic WebAPI-project with visual studio, it will include some samples and you can get going from that. If you run into wall, you can come back here
Is it possible to POST "url encoded" parameters to a remote web service instead of JSON or XML ?
My rails application consumes a web service which takes URL encoded parameters (content-type: application/x-www-form-urlencoded) in POST requests and give JSON answers.
Is this kind of RESTful services common ?
When you make a hit to a JSON or XML web service using Ajax then the parameters are just getting encoded as either GET or POST, and is typically sent using the application/x-www-form-urlencoded content type anyway (see http://api.jquery.com/jQuery.ajax/ for an example specific to jQuery).
So, basically, yes, it is possible to send data in any format (JSON, XML, BSON etc.) in this manner.
I found the MVC API Action filter which should automatically check if the request demands a json response and if so automatically serialize in json the model i was sending to the view, right ?!
http://mvcapi.codeplex.com/
I found many examples but the thing is they all assume that the request will be sent by an Ajax call in which i can clearly specify it's a json request.
I want to call the action directly from my browser but i'm not without any specification it simply returns the view
How do i specify in the url that i'm requesting a json response?
I know of the library that you write of, but haven't used it. I shied away from using it when I saw that it never seems to have made it out of Beta on Codeplex and hasn't been updated in over a year.
That aside, in the methodology being used, the URL doesn't determine the data type coming back, the Http Accept Headers are what does it. This is a more RESTful approach to returning data.
You'll note on the link that you provided in the Request section that
Accept: application/json, text/javascript, */*
application/json is what tells the service to return json. You may find other examples on the web that say text/json instead, and they should work also, but application/json is the correct standard.
If you're using jQuery, you can use $.ajax and specify dataType: 'json' or just use the $.getJson method directly.