I am getting this error running an actix-web based server
ERROR actix_http::h1::dispatcher] stream error: Request parse error: Invalid Header provided
The handler code is this:
#[derive(Serialize, Deserialize)]
pub struct Data {
some_data: String
};
async fn handler_post(
request: HttpRequest,
data: web::Json<Data>
) -> impl Responder {
HttpResponse::OK()
.json(ApiResponse {
status: "success"
})
}
The headers being sent are accept, Content-Type and User-Agent. I don't know how to make it work. By the way, i'm using actix-web 4.
I tried the handler with actix version 4 and facing no issue
src/main.rs
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest};
use serde::{Serialize, Deserialize};
#[get("/")]
async fn hello() -> impl Responder {
HttpResponse::Ok().body("Hello world!")
}
#[derive(Serialize, Deserialize)]
pub struct Data {
some_data: String
}
#[derive(Serialize, Deserialize)]
pub struct ApiResponse<'a> {
status: &'a str
}
#[post("/")]
async fn handler_post(
request: HttpRequest,
data: web::Json<Data>
) -> impl Responder {
HttpResponse::Ok()
.json(ApiResponse {
status: "success"
})
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(handler_post)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Cargo.toml
[package]
name = "..."
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix-web = "4"
serde = { version = "1.0", features = ["derive"] }
Related
I want to create a actix-web server where I can provide my Search trait as application data in order to easily swap between multiple implementations or use mock implementation for testing. Whatever I try I can't get it to compile or when I get it to compile I get the following error when visiting the route in the web browser:
App data is not configured, to configure use App::data()
Here is what I have so far
# Cargo.toml
[dependencies]
actix-rt = "1.1.1"
actix-web = "3.3.2"
[dev-dependencies]
tokio = "0.2.22"
//! main.rs
use actix_web::dev::Server;
use actix_web::{get, web, App, HttpServer, Responder};
pub trait Search {
fn search(&self, query: &str) -> String;
}
#[derive(Clone)]
pub struct SearchClient {
base_url: String,
}
impl SearchClient {
pub fn new() -> Self {
Self {
base_url: String::from("/search"),
}
}
}
impl Search for SearchClient {
fn search(&self, query: &str) -> String {
format!("Searching in SearchClient: {}", query)
}
}
#[get("/{query}")]
async fn index(
web::Path(query): web::Path<String>,
search: web::Data<dyn Search>,
) -> impl Responder {
search.into_inner().search(&query)
}
pub fn create_server(
search: impl Search + Send + Sync + 'static + Clone,
) -> Result<Server, std::io::Error> {
let server = HttpServer::new(move || App::new().data(search.clone()).service(index))
.bind("127.0.0.1:8080")?
.run();
Ok(server)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let search_client = SearchClient::new();
create_server(search_client).unwrap().await
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Clone)]
pub struct TestClient;
impl Search for TestClient {
fn search(&self, query: &str) -> String {
format!("Searching in TestClient: {}", query)
}
}
#[actix_rt::test]
async fn test_search() {
let search_client = TestClient {};
let server = create_server(search_client).unwrap();
tokio::spawn(server);
}
}
When adding the data to your App, you have to specify that you want it to be downcasted as a trait object. Data does not accept unsized types directly, so you have to first create an Arc (which does accept unsized types) and then convert it to a Data. We will use the app_data method to avoid wrapping the searcher in a double arc.
pub fn create_server(
search: impl Search + Send + Sync + 'static,
) -> Result<Server, std::io::Error> {
let search: Data<dyn Search> = Data::from(Arc::new(search));
HttpServer::new(move || {
App::new()
.app_data(search.clone())
})
}
async fn index(
query: Path<String>,
search: Data<dyn Search>,
) -> impl Responder {
search.into_inner().search(&*query)
}
An alternative approach is using generics. Your handler and create_server functions would be generic over a Search implementation:
async fn index<T: Search>(
web::Path(query): web::Path<String>,
search: web::Data<T>,
-> impl Responder {
search.into_inner().search(&query)
}
pub fn create_server<T: Search + Send + Sync + 'static + Clone>(
search: T,
) -> Result<Server, std::io::Error> {
let server = HttpServer::new(move || {
App::new()
.data(search.clone())
.route("/{query}", web::get().to(index::<T>))
})
.bind("127.0.0.1:8080")?
.run();
Ok(server)
}
Now, when you create the server in main, you can use SearchClient:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let search_client = SearchClient::new();
create_server(search_client).unwrap().await
}
And when you create the server for testing purposes, you could use a TestClient:
#[actix_rt::test]
async fn test_search() {
let search_client = TestClient {};
let server = create_server(search_client).unwrap();
}
The downside to the generics based approach is that you cannot use the #[get("")] macros for routing because you have to specify the handler's generic parameters:
App::new()
.route("/{query}", web::get().to(index::<T>))
I want to use state within my guards. I want to have routes that required authentication with an api key which I want to define in my Rocket.toml. But running this code I get the following error:
the trait From<(Status, ())> is not implemented for (Status, ApiKeyError)
for this line of code let config_state = try_outcome!(req.guard::<State<'_, Config>>().await);
How do I implement this trait? Or is there even a better solution to manage the api token in Rocket.
I am using the 0.5.0-devversion of Rocket.
#[macro_use] extern crate rocket;
use rocket::http::Status;
use rocket::request::{Outcome, Request, FromRequest};
use rocket::State;
use rocket::fairing::AdHoc;
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
api_key: String,
}
struct ApiKey<'r>(&'r str);
#[derive(Debug)]
enum ApiKeyError {
Missing,
Invalid,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for ApiKey<'r> {
type Error = ApiKeyError;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
let config_state = try_outcome!(req.guard::<State<'_, Config>>().await);
/// Returns true if `key` is a valid API key string.
fn is_valid(key: &str, api_key: String) -> bool {
key == api_key
}
match req.headers().get_one("Authorization") {
None => Outcome::Failure((Status::Unauthorized, ApiKeyError::Missing)),
Some(key) if is_valid(key, config_state.api_key) => Outcome::Success(ApiKey(key)),
Some(_) => Outcome::Failure((Status::Unauthorized, ApiKeyError::Invalid)),
}
}
}
#[get("/")]
async fn index(config: State<'_, Config>, key: ApiKey<'_>) -> &'static str {
"Hello, world!"
}
fn rocket() -> rocket::Rocket {
let rocket = rocket::ignite();
let figment = rocket.figment();
let config: Config = figment.extract().expect("config");
rocket
.mount("/", routes![index])
.attach(AdHoc::config::<Config>())
}
#[rocket::main]
async fn main() {
rocket()
.launch()
.await;
}
I already stored the config with AdHoch::config() but to retrieve it within the guard I need to use request.rocket().state::<Config>(). The corrected source code is below:
#[macro_use] extern crate rocket;
use rocket::http::Status;
use rocket::request::{Outcome, Request, FromRequest};
use rocket::State;
use rocket::fairing::AdHoc;
use serde::Deserialize;
#[derive(Deserialize)]
struct Config {
api_key: String,
}
struct ApiKey<'r>(&'r str);
#[derive(Debug)]
enum ApiKeyError {
Missing,
Invalid,
}
#[rocket::async_trait]
impl<'r> FromRequest<'r> for ApiKey<'r> {
type Error = ApiKeyError;
async fn from_request(req: &'r Request<'_>) -> Outcome<Self, Self::Error> {
// Retrieve the config state like this
let config = req.rocket().state::<Config>().unwrap();
/// Returns true if `key` is a valid API key string.
fn is_valid(key: &str, api_key: &str) -> bool {
key == api_key
}
match req.headers().get_one("Authorization") {
None => Outcome::Failure((Status::Unauthorized, ApiKeyError::Missing)),
Some(key) if is_valid(key, &config.api_key) => Outcome::Success(ApiKey(key)),
Some(_) => Outcome::Failure((Status::Unauthorized, ApiKeyError::Invalid)),
}
}
}
#[get("/")]
async fn index(config: State<'_, Config>, key: ApiKey<'_>) -> &'static str {
"Hello, world!"
}
fn rocket() -> rocket::Rocket {
let rocket = rocket::ignite();
let figment = rocket.figment();
let config: Config = figment.extract().expect("config");
rocket
.mount("/", routes![index])
.attach(AdHoc::config::<Config>())
}
#[rocket::main]
async fn main() {
rocket()
.launch()
.await;
}
Better late than never and so I started re-learning Rust and decided to focus on actix and actix-web.
I have these codes running in actix-web 1.0 and it seems not to run in actix-web 3.0:
main.rs
use messages_actix::MessageApp;
fn main() -> std::io::Result<()> {
std::env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
let app = MessageApp::new(8081);
app.run() // error here
}
error: "no method named run found for opaque type impl std::future::Future in the current scope method not found in impl std::future::Future
lib.rs
#[macro_use]
extern crate actix_web;
use actix_web::{middleware, web, App, HttpRequest, HttpServer, Result};
use serde::Serialize;
pub struct MessageApp {
pub port: u16,
}
#[derive(Serialize)]
pub struct IndexResponse{
pub message: String,
}
#[get("/")]
pub fn index(req: HttpRequest) -> Result<web::Json<IndexResponse>> { // error here
let hello = req
.headers()
.get("hello")
.and_then(|v| v.to_str().ok())
.unwrap_or_else(|| "world");
Ok(web::Json(IndexResponse {
message: hello.to_owned(),
}))
}
error for index: the trait Factory<_, _, _> is not implemented for fn(HttpRequest) -> std::result::Result<Json<IndexResponse>, actix_web::Error> {<index as HttpServiceFactory>::register::index}
impl MessageApp {
pub fn new(port: u16) -> Self {
MessageApp{ port }
}
pub fn run(&self) -> std::io::Result<()> {
println!("Starting HTTP server at 127.0.0.1:{}", self.port);
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(index)
})
.bind(("127.0.0.1", self.port))?
.workers(8)
.run() //error here
}
}
error: expected enum std::result::Result, found struct Server
checked the migration doc yet can't find what I'm looking for in relation to the errors listed.
Any help greatly appreciated...Thanks...
Newer versions of actix-web now use the async-await syntax, which was made stable as of Rust 1.39. You have to make your handlers async:
#[get("/")]
pub async fn index(req: HttpRequest) -> Result<web::Json<IndexResponse>> {
// ...
}
Creating an HttpServer is now an async operation:
impl MessageApp {
pub fn run(&self) -> std::io::Result<()>
HttpServer::new(...)
.run()
.await
}
}
And you can use the main macro to use async/await in your main function:
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let app = MessageApp::new(8081);
app.run().await
}
With the example provided in the documentation for actix_web::web::Query, how can I make the response_type resort to None when providing an unknown variant?
If I have the following:
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
#[derive(Deserialize)]
pub struct AuthRequest {
id: u64,
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
And I visit http://localhost:8080/?id=1&response_type=foo, I get this 400 response:
Query deserialize error: unknown variant foo, expected Token or Code
When I instead would like it to only accept the values of the Enum as valid values, and if no value or an invalid value is provided I want it to be set to None.
This can be dealt with deserialize_with.
use actix_web::{web, App, HttpServer};
use serde::Deserialize;
use serde::de::{Deserializer};
#[derive(Debug, Deserialize)]
pub enum ResponseType {
Token,
Code,
}
fn from_response_type<'de, D>(deserializer: D) -> Result<Option<ResponseType>, D::Error>
where
D: Deserializer<'de>,
{
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);
Ok(res)
}
#[derive(Debug, Deserialize)]
pub struct AuthRequest {
id: u64,
#[serde(deserialize_with = "from_response_type")]
response_type: Option<ResponseType>,
}
async fn index(web::Query(info): web::Query<AuthRequest>) -> String {
format!(
"Authorization request for client with id={} and type={:?}!",
info.id, info.response_type
)
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind(("127.0.0.1", 8080))?
.run()
.await
}
Any invalid value is considered a None. The key line being
let res: Option<ResponseType> = Deserialize::deserialize(deserializer).unwrap_or(None);
I'm storing data from an external service in a local cache and I want to create an endpoint to return data currently in the cache.
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use]
extern crate rocket;
use rocket::{Route, State};
use rocket_contrib::json::Json;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Post {
pub title: String,
pub body: String,
}
impl Post {
pub fn new(title: String, body: String) -> Post {
Post { title, body }
}
}
pub struct Cache {
pub posts: Vec<Post>,
}
impl Cache {
pub fn new() -> Cache {
let mut posts = vec![
Post::new(String::from("Article1"), String::from("Blah")),
Post::new(String::from("Article2"), String::from("Blah")),
Post::new(String::from("Article3"), String::from("Blah")),
Post::new(String::from("Article4"), String::from("Blah")),
Post::new(String::from("Article5"), String::from("Blah")),
];
Cache { posts }
}
}
#[derive(Responder)]
pub enum IndexResponder {
#[response(status = 200)]
Found(Json<Vec<Post>>),
#[response(status = 404)]
NotFound(String),
}
#[get("/")]
pub fn index(cache: State<Cache>) -> IndexResponder {
return IndexResponder::Found(Json(cache.posts));
}
fn main() {
rocket::ignite()
.mount("/", routes![index])
.manage(Cache::new())
.launch();
}
The compiler complains:
error[E0507]: cannot move out of dereference of `rocket::State<'_, Cache>`
--> src/main.rs:50:39
|
50 | return IndexResponder::Found(Json(cache.posts));
| ^^^^^^^^^^^ move occurs because value has type `std::vec::Vec<Post>`, which does not implement the `Copy` trait
(cargo build --verbose output)
My Cargo.toml file:
[package]
name = "debug-project"
version = "0.1.0"
authors = ["Varkal <mail#example.com>"]
edition = "2018"
[dependencies]
rocket = "0.4.0"
rocket_contrib = "0.4.0"
serde = { version = "1.0.90", features = ["derive"] }
serde_json = "1.0.39"
A very simple repository to reproduce the error.
Your endpoint does not own the state and thus it cannot return an owned Vec<Post>. Conceptually, this makes sense because if you did take ownership of it, then what value would be present the next time the endpoint is called?
The simplest thing you can do is clone the data:
#[get("/")]
pub fn index(cache: State<Cache>) -> IndexResponder {
IndexResponder::Found(Json(cache.posts.clone()))
}
This will require that you implement Clone for Post, or perhaps change your state to hold something like an Arc.
A slightly more performant solution is to return a slice that refers to the state. This requires no cloning, but does require using the State::inner method:
#[derive(Responder)]
pub enum IndexResponder<'a> {
#[response(status = 200)]
Found(Json<&'a [Post]>),
#[response(status = 404)]
NotFound(String),
}
#[get("/")]
pub fn index<'a>(cache: State<'a, Cache>) -> IndexResponder<'a> {
IndexResponder::Found(Json(&cache.inner().posts))
}
See also:
Cannot move out of borrowed content when trying to transfer ownership
What happens when an Arc is cloned?