NOTE : Question reformulated to be more clear
I would like to simulate the sending of a form to an external domain in a controller action (like a redirect_to an external domain in POST with POST params). IE : Send the POST request with the params, and render the HTML results in the browser.
What is the best way to do it in Rails 3?
Sure, you can just execute the POST on the server side, collect the response and then do whatever you like with it. It's not unlike making a server side call to your favourite REST API and then rendering the response to the client.
You probably want to start with a library that provides a nice interface for server side POST. I'd suggest looking at https://github.com/nahi/httpclient
Related
I am new to designing REST APIs. I have trying to create following APIs for online game play
GET domain/api/games // return all games
POST domain/api/games // create a new game on behalf of current user
Now my issue is that when I try to create game using POST, I want userId to be sent to the API. I am not sure how to do this. Also note that I have another get API to get details of individual game, something like
GET domain/api/games/{gameId}
so I cannot pass userId to POST like domain/api/games/{useID} as it will conflict will above API.
So how do I pass usedId to POST. Also I don't want to use query params. Any suggestions to design this would be great.
When you are making a POST to a service, the parameters you communicate are known as BODY params, they don't go on the query string.
Different technologies have different APIs for interacting with POST params, but the underlying theory is the same, and is described by the W3C HTTP standard
http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html
The specifics of how to use POST params vary depending on the language and technology you're using. For example, if you are using jquery, there are a couple different ways to do it, with with the $.post('url', data, callback) method or with the $.ajax(...) option.
http://api.jquery.com/jquery.post/
http://api.jquery.com/jquery.ajax/
When reading POST params on the server, you'll generally access them using some some sort of request object, that will store your parameters in memory for you to access. This is highly dependent of the language and framework you're using, but here are links to some common ones:
NodeJS/express: http://expressjs.com/4x/api.html#request
PHP: http://php.net/manual/en/reserved.variables.post.php
ASP.Net: http://msdn.microsoft.com/en-us/library/system.web.httprequest.params(v=vs.110).aspx
Java/Spring: https://spring.io/guides/gs/handling-form-submission/
It should be either part of the context (you can pass it through header) or part of the game object. I prefer the context option, the httpheader can contain some auth bearer token so that you can figure out the user on the backend through the token.
I am currently working on Rails.I have read a lot of documents regarding REST api and RESTFul routes.But i am unable to figure out the relation between these two.My understanding for RESTFul routes is that a http verb is associated with them and i found out nothing else.
Can someone please make these things clear?
1.What are RESTFul routes and their benefits?
2.In what way RESTFul routes are co-related with Rest Api?
3.What are the benefits of REST api over SOAP api?
I don't know Rails, but I can tell you the following:
1)The routes are defined in order to map an incoming request to a controller action. A request is made of an HTTP method (GET/PUT/POST/DELETE), an URL and possibly some content in the body. For example GET books/1234 is a RESTful request for the book identified by the ISBN 1234. A framework like Rails has to map this request to a controller action. Such an action is an operation whose name is a verb. In the router you have 2 main components for an entry: the request and the mapped action. For example, in get '/books/:isbn', to: 'books#getBookByIsbn' what you say is that a GET request with an URL matching the given structure will be mapped to the action called getBookByIsbn from the controller books.
2)One benefit of RESTful services is that IMHO they are simpler, easier to user than SOAP/WSDL based services.
I have a task to do where I need to make calls to an external xml api to fetch data for an event calendar in the sidebar of a site. The date will be changed with JavaScript and then i need to make another call to refresh the data. Can somebody give me an idea about how to cleanly set up an action or function somewhere that I can direct an Ajax action to? It's easy to set up a widget with the correct HTML etc but where does the Ajax connect to? Ideally when the content initially loads on the page it would use the same function that the post is going to use to generate the HTML on the server side.
Any tips would be appreciated. This is an xml api...no option for json or jsonp so credentials including a token and user I'm assuming will have to go somewhere in my widget, something like a proxy function?
It sounds as if you're asking about making cross-domain AJAX requests, AKA the "same origin policy."
The same origin policy prevents document or script loaded from one origin from getting or setting properties of a document from a different origin (domain). See http://www.mozilla.org/projects/security/components/same-origin.html for a more detailed description of the policy.
See Ways to circumvent the same-origin policy for a good description of the options available to circumvent this limitation.
The short answer is that unless you have control over the domain to which you're making AJAX requests, your best bet is probably to set up a simple proxy that lives in the same domain where your AJAX is running, which will forward requests to the destination. Doing a google search on "simple AJAX proxy" will get you a host of results, including pre-built proxies in a variety of languages.
I'm trying to implement a javascript tracker that i need to inject arbitrarily on all of the views rendered by my application, just like the newrelic client instrumentation works.
My app allows user to edit their liquid html templates, so the idea doing this is to inject the script in a way that the user is uncapable to remove it (auto added)
I look the code in the newrelic gem but is too confusing and i wondering if there is a more simple way to do it.
Thanks in advance!
Well I have a solution for this you could write a middleware where you could just check
if the request is for html page or (css or javascript)
if the request is for the html page
append the javascript to the html page before sending the response for the server
here the catch you need very sure what you are doing this I have ran into this problem
Make sure your middleware placement is correct since the development everything is single thread and wrong placement would result in deadlock error
When HTML page is consider what if the request is an ajax request what then you have to be very specific on that regards
Hope This help
I'm running into a dilemma here. The Firefox Add-Ons SDK only allows PUT and POST requests via its API. I am using the following route in Rails: "resource :users"
I can easily override the PUT to be:
"match 'users/:id' => 'users#update', :via => [:post, :put]
However, I can't do this again for DELETE as I also need to :post to the same URL again. Do I need to create a new route all-together and abandon the resourceful way of doing things? Is there a way to have POST behave like a DELETE on the Firefox side instead?
There is currently no way to do this in the high-level Request api, however you could implement something using the lower level xhr api:
https://addons.mozilla.org/en-US/developers/docs/sdk/1.2/packages/api-utils/docs/xhr.html
This provides access to Firefox's underlying XmlHttpRequest implementation:
https://developer.mozilla.org/En/Using_XMLHttpRequest