-
Notifications
You must be signed in to change notification settings - Fork 3
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
Failed to extract Data<awc::client::Client>
for proxy
#17
Comments
Well, the samples from the README are just snippets showing the relevant parts of how to use the traits of this crate. What they don't show is that you need to add a use awc::Client;
use actix_web::{get, web, HttpResponse};
use actix_proxy::{IntoHttpResponse, SendRequestError};
#[get("/{url:.*}")]
async fn proxy(
path: web::Path<(String,)>,
client: web::Data<Client>,
) -> Result<HttpResponse, SendRequestError> {
let (url,) = path.into_inner();
let url = format!("https://duckduckgo.com/{url}");
// here we use `IntoHttpResponse` to return the request to
// duckduckgo back to the client that called this endpoint
Ok(client.get(&url).send().await?.into_http_response())
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.app_data(web::Data::new(Client::default())) // This is the important line
.service(proxy)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
} How can I improve documentation to make it clear that you are required to add an instance of |
Hello! Yes I think a fully working example would be helpful for other clueless people like me. I really enjoyed this other crate for example:
(I think this I remember trying to look at the tests in In the end I went with the NodeJS cors-anywhere project, which is what I was trying to achieve with this package at the time (fetching a resource and removing CORS limitations to proxy it to the browser). |
Tried to run the sample code in README, this happens:
What did I do wrong?
upset that the basic code doesn't work haha
The text was updated successfully, but these errors were encountered: