Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I respond with redirects? #221

Closed
freiguy1 opened this issue May 18, 2015 · 8 comments
Closed

How do I respond with redirects? #221

freiguy1 opened this issue May 18, 2015 · 8 comments

Comments

@freiguy1
Copy link

I'm manually returning a redirect response by directly manipulating the header, but I do not like the way I'm doing so:

router.get("**", middleware! { |request, mut response| 
    response.set_status(nickel::status::StatusCode::TemporaryRedirect);
    response.origin.headers_mut().set_raw("location", vec![b"https://github.com/freiguy1/schedule-gen-web".to_vec()]);
    return response.send("");
});

What's the preferred way to redirect? The last time I checked, some code for redirecting in examples was commented out.

@Ryman
Copy link
Member

Ryman commented May 18, 2015

use hyper::header::Location;
use nickel::status::StatusCode;
... middleware! { |request, mut response|
    response.origin.headers_mut().set(Location("https://...".into()));
    (StatusCode::TemporaryRedirect, "")
}

Will be cleaner once #217 lands: Ryman@37482ad. As we evolve I'm sure we'll develop a shorthand for this scenario, e.g. res.redirect("url").

If you have any other feedback on that thread that's also appreciated!

@freiguy1
Copy link
Author

I get this error when trying it using the built in Location header. I got to this point before, and ended up doing it the other way. Am I missing some trait usings?

My Cargo.toml looks like:

[package]
name = "schedule-gen-web"
version = "0.0.1"
authors = ["Ethan Frei <[email protected]>"]

[dependencies.nickel]

git = "https://github.com/nickel-org/nickel.rs.git"

[dependencies.schedule_gen]

git = "https://github.com/freiguy1/schedule-gen-rs"

[dependencies]

rustc-serialize = "*"

hyper = "*"
src/main.rs:19:39: 19:107 error: the trait `hyper::header::Header` is not implemented for the type `hyper::header::common::location::Location` [E0277]
src/main.rs:19         response.origin.headers_mut().set(Location("https://github.com/freiguy1/schedule-gen-web".into()));
                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<nickel macros>:1:1: 1:27 note: in expansion of as_block!
<nickel macros>:14:1: 14:31 note: expansion site
note: in expansion of closure expansion
<nickel macros>:12:6: 14:43 note: expansion site
<nickel macros>:1:1: 14:51 note: in expansion of middleware__inner!
<nickel macros>:2:1: 2:64 note: expansion site
<nickel macros>:1:1: 7:47 note: in expansion of middleware!
src/main.rs:18:22: 21:6 note: expansion site
src/main.rs:19:39: 19:107 error: the trait `hyper::header::HeaderFormat` is not implemented for the type `hyper::header::common::location::Location` [E0277]
src/main.rs:19         response.origin.headers_mut().set(Location("https://github.com/freiguy1/schedule-gen-web".into()));
                                                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<nickel macros>:1:1: 1:27 note: in expansion of as_block!
<nickel macros>:14:1: 14:31 note: expansion site
note: in expansion of closure expansion
<nickel macros>:12:6: 14:43 note: expansion site
<nickel macros>:1:1: 14:51 note: in expansion of middleware__inner!
<nickel macros>:2:1: 2:64 note: expansion site
<nickel macros>:1:1: 7:47 note: in expansion of middleware!
src/main.rs:18:22: 21:6 note: expansion site
error: aborting due to 2 previous errors
Could not compile `schedule-gen-web`.

To learn more, run the command again with --verbose.

@Ryman
Copy link
Member

Ryman commented May 19, 2015

Does it compile if you set hyper = "=0.3" within Cargo.toml?

@freiguy1
Copy link
Author

That did build and work! Thanks.

@anvie
Copy link

anvie commented Sep 8, 2015

I got this:

src/main.rs:236:13: 236:28 error: field `origin` of struct `nickel::response::Response` is private
src/main.rs:236             response.origin.headers_mut().set(Location("/".to_string()));
                            ^~~~~~~~~~~~~~~

@Ryman
Copy link
Member

Ryman commented Sep 8, 2015

it should be just response.headers_mut().set(..) now :)

Or even, response.set(Location(..)) (assuming you import the modifier trait)

@anvie
Copy link

anvie commented Sep 8, 2015

woah, that's was quick.
is this has same effect?:

response.set(Location("/".into()));

@Ryman
Copy link
Member

Ryman commented Sep 8, 2015

It should be :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants