rust-rocket

How can I set the HTTP status code of a (Rust) Rocket API endpoint's Template response?

白昼怎懂夜的黑 提交于 2021-01-27 17:24:54
问题 I have the following login POST endpoint handler in my Rocket API: #[post("/login", data = "<login_form>")] pub fn login_validate(login_form: Form<LoginForm>) -> Result<Redirect, Template> { let user = get_user(&login_form.username).unwrap(); match user { Some(existing_user) => if verify(&login_form.password, &existing_user.password_hash).unwrap() { return Ok(Redirect::to(uri!(home))) }, // we now hash (without verifying) just to ensure that the timing is the same None => { hash(&login_form

How can I set the HTTP status code of a (Rust) Rocket API endpoint's Template response?

夙愿已清 提交于 2021-01-27 17:18:47
问题 I have the following login POST endpoint handler in my Rocket API: #[post("/login", data = "<login_form>")] pub fn login_validate(login_form: Form<LoginForm>) -> Result<Redirect, Template> { let user = get_user(&login_form.username).unwrap(); match user { Some(existing_user) => if verify(&login_form.password, &existing_user.password_hash).unwrap() { return Ok(Redirect::to(uri!(home))) }, // we now hash (without verifying) just to ensure that the timing is the same None => { hash(&login_form

How to set up CORS or OPTIONS for Rocket.rs

纵然是瞬间 提交于 2020-11-29 09:21:05
问题 I've got a backend running rocket.rs which my flutter web app sends a request to, but it can't get past the OPTIONS response. I have tried adding CORS (rocket_cors) to the backend and having a options response, but it still sends back: Error: XMLHttpRequest error. dart:sdk_internal 124039:30 get current packages/http/src/browser_client.dart.lib.js 214:124 <fn> I have added the following to my rocket project: #[options("/")] fn send_options<'a>(path: PathBuf) -> Response<'a> { let mut res =

Return JSON with an HTTP status other than 200 in Rocket

邮差的信 提交于 2020-04-07 03:46:02
问题 I want my Rocket API to have a route like this: #[post("create/thing", format = "application/json", data="<thing>")] When the client sends { "name": "mything" } , everything should be alright and I know how to do that, but when it sends { "name": "foo" } it should respond with something like this: HTTP/1.1 422 Unprocessable Entity Content-Type: application/json { "errors": [ { "status": "422", "title": "Invalid thing name", "detail": "The name for a thing must be at least 4 characters long."

Return JSON with an HTTP status other than 200 in Rocket

心不动则不痛 提交于 2020-04-07 03:44:12
问题 I want my Rocket API to have a route like this: #[post("create/thing", format = "application/json", data="<thing>")] When the client sends { "name": "mything" } , everything should be alright and I know how to do that, but when it sends { "name": "foo" } it should respond with something like this: HTTP/1.1 422 Unprocessable Entity Content-Type: application/json { "errors": [ { "status": "422", "title": "Invalid thing name", "detail": "The name for a thing must be at least 4 characters long."

How do I fix mismatching dependencies in my Cargo file to work around native library collisions?

巧了我就是萌 提交于 2020-03-16 06:49:38
问题 I'm setting up a Rust server with Rocket and I'm trying to use it with a JWT library. They use different versions of the *ring* crate and I get an error during cargo build : error: multiple packages link to native library `ring-asm`, but a native library can be linked only once package `ring v0.12.1` ... which is depended on by `jsonwebtoken v4.0.1` ... which is depended on by `auther v0.1.0 (file:///home/drpytho/x/downloadble/auther)` links to native library `ring-asm` package `ring v0.11.0`

How can I pass a variable initialized in main to a Rocket route handler?

*爱你&永不变心* 提交于 2019-12-20 06:02:28
问题 I have a variable that gets initialized in main (line 9) and I want to access a reference to this variable inside of one of my route handlers. #[get("/")] fn index() -> String { return fetch_data::fetch(format!("posts"), &redis_conn).unwrap(); // How can I get redis_conn? } fn main() { let redis_conn = fetch_data::get_redis_connection(); // initialized here rocket::ignite().mount("/", routes![index]).launch(); } In other languages, this problem would be solvable by using global variables. 回答1

Compiler says that data cannot be shared between threads safely even though the data is wrapped within a Mutex

拜拜、爱过 提交于 2019-12-19 09:45:17
问题 I'm using Rocket which has a State that it passes to the HTTP requests. This struct contains a Mutex<DatastoreInstance> which gives access to a SQLite database and is locked with a mutex to make read and writes safe. pub struct DatastoreInstance { conn: Connection, } When the DatastoreInstance struct looked like this, with only a SQLite connection everything worked fine, but I then also wanted to add a transaction object within this struct: pub struct DatastoreInstance { conn: Connection,

Lifetimes when Deserializing JSON within a FromForm

空扰寡人 提交于 2019-12-11 04:56:07
问题 I'm having trouble understanding the relationship between the lifetimes on this code. Basically, I have a Rocket API that receives some x-www-form-urlencoded data, with only one key: json . This key contains, intuitively, a JSON value, encoded with percent-encoding, of a struct Message<T> . (I'm aware this is suboptimal API design, but this is reverse-engineering work, so I have no option) To be easily used as a request guard as From<Message<T>> , I'm implementing FromForm . To do that, I