(Rust, Actix Web) How to change method when using actix_web::web::Redirect::to(url)? - post

I am trying to handle a POST request to the /upload path. Once everything is done, I want to redirect to the main page. However, when I do that, it uses the POST method (as shown in the picture). Is there a way to redirect using the GET method?
Redirect to main page uses POST method
Error Message:
POST http://0.0.0.0/ [HTTP/1.1 405 Method Not Allowed 0ms]
#[post("/upload")]
async fn upload(req_body: String) -> impl Responder {
// ...
// This redirects to homepage but uses POST. Is there a way to redirect to GET http://0.0.0.0/?
actix_web::web::Redirect::to("http://0.0.0.0/")
}
I've tried using the methods in the Redirect Crate but it still redirects with POST
I've tried implementing redirect directly from the http form from where I'm sending the POST request. This is what I tried

Redirects are "307 Temporary Redirect" by default. Those keep the method for accessing the next page. You want "303 See Other" by using the see_other method, which will convert all methods to GET.
actix_web::web::Redirect::to("http://0.0.0.0/").see_other()
This link from the Redirect documentation explains more.

Related

What is the difference between Redirect and RedirectToAction in ASP.NET MVC?

What is the difference between Redirect and RedirectToAction other than their return type?
When do we use each? Explanation with any real life scenario would help me greatly.
I was looking at Confusion between Redirect and RedirectToAction, but, to me, it looks like the answer is more specific towards handling id parameter and returning proper view.
RedirectToAction lets you construct a redirect url to a specific action/controller in your application, that is, it'll use the route table to generate the correct URL.
Redirect requires that you provide a full URL to redirect to.
If you have an action Index on controller Home with parameter Id:
You can use RedirectToAction("Index", "Home", new { id = 5 }) which will generate the URL for you based on your route table.
You can use Redirect but must construct the URL yourself, so you pass Redirect("/Home/Index/5") or however your route table works.
You can't redirect to google.com (an external URL) using RedirectToAction, you must use Redirect.
RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table.
Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.
Best Practices: Use RedirectToAction for anything dealing with your application actions/controllers. If you use Redirect and provide the URL, you'll need to modify those URLs explicitly when your route table changes.

Page not getting rendered via Express when using post (NodeJS)

I am using Express v.2.4.6 (Node.js - v.0.6.2).
I am trying to render (or redirect to) a new page once POST is called (as opposed to GET). I am able to render/redirect when GET is called. However, I cannot seem to render a page when POST is called in Express. I am not sure if this is even possible though the guide on the Express site does mention an example where you can redirect once POST is called.
The relevant code is given below (client is sending the form in JSON). I can parse through the JSON message successfully in Node.j.
Sample route:
app.post('/signup', function(req, res){
res.redirect('index');
//res.render('index');
});
No exceptions are thrown, but the index page does not get rendered nor redirected.
Any feedback will be appreciated.
Maybe you call it from $.ajax from client-side
It works well if you call it from server-side
Please refer to this question.
Express.js Won't Render in Post Action
res.redirect('/') is most likely what you wanted there, but you can render in any route, redirecting is just a convention that most people use

JSF login forward address bar / url -- redirect does not solve problem

Okay we have a single - sign - on and the user will likely enter www.blabla.com/AppName/ to reach our site. We then define a welcome site, use a phaselistener to check:
is user trying to access the welcome site? yes -> try to login - works? yes -> get user roles -> forward to the appropriate site for this specific user.
E.g. user niceBelly goes to page /somewhere/in/many/folders/beer.jsf and user barbie goes to /breasts/pink.jsf
a redirect is in this application not possible for some reasons.
the result is that when reaching e.g. page pink.jsf the address bar still shows blablaba.com/AppName/
clicking the first link will result in the browser using the form address as new URL e.g. on welcome.jsf i navigate to coolstuff.jsf. On the page coolstuff i now have the url of the last form, e.g. welcome.jsf. Then on cool stuff i click a link, and get coolstuff on the next page as url, and so on.
Is there a way to solve this / work around it?
Given the symptoms, you are actually not redirecting the requests, but you are actually forwarding the requests. A real redirect will take place when you call
externalContext.redirect(url);
in JSF context, or when you add
<redirect />
to the navigation case. All other ways are effectively forwards. As per the symptoms, you're using commandlinks instead of outputlinks to navigate to other page. Commandlinks will submit a POST request to current URL and JSF will under the covers use RequestDispatcher to set the destination of the request/response when the navigation case doesn't contain <redirect />. A forward does not instruct the browser to fire a new GET request on the destination URL and hence the URL in browser address bar does not change. A real redirect will do exactly that.
See also:
When should I use outputlink instead of commandlink?
Bookmarkable URLs in JSF 1.x

rails ajax redirect

Here is my use case
I have a search model with two actions search_set and search_show.
1 - A user loads the home page which contains a search_form, rendered
via a partial (search_form).
2 - User does a search, and the request goes to search_set, the search
is saved and a redirect happens to search_show page which again
renders the search_form with the saved search preferences. This search
form is different than the one if step1, because it's a remote form
being submitted to the same action (search set)
3 - Now the user does another search, and the search form is submitted
via ajax to the search_set action. The search is saved and executed
and now I need to present the result via rjs templates (corresponding
to search_show). I am told that if the request is xhr then I can't
redirect to the search_show action? Is that right? If yes, how do I
handle this?
Here is my controller class
http://pastie.org/993460
Thanks
That's right. Either make the request non-XHR and redirect as normal, or you could try rendering the URL you want to redirect to as text or part of a JSON object which your AJAX request then uses to call document.location.href = [whatever] (but this seems hacky).
Right now what's happening is your XHR request is returning the result of the redirect, and not actually redirecting the page that made the XHR request.

Questions about grails filters

Basically I have 2 questions regarding grails filters.
According to grails documentation you can do something like below in a filter
if (userId != paramsUserId) {
flash.message = "You can only modify yourself"
redirect(action: 'list')
return false
}
If the above condition is true then how will the return statement get executed ?
Can I have a redirect in my filter to a some action which also has a redirect ?
1 - Returning false from a filter prevents further filters (and the action if it's in a before filter) from executing. The browser would get the 302 redirect and go to the 'list' method that you've asked to redirect to.
http://grails.org/doc/2.3.7/guide/single.html#filterTypes
2 - yep. redirecting to something else that redirects is fine. It really doesn't matter to the browser. If you watch it in firebug, you'll see what a redirect really is. When you redirect the browser receives a response with an HTTP status code of 302 ("Found"), this response also includes the url that the browser should request next (the thing you're redirecting to, i.e. the url for the "list" method in the example above). The browser then requests that url and it behaves as if it were the first request.
This is why flash scope is so useful, things in flash scope live until the next request, so they span redirects.

Resources