How to set up CORS or OPTIONS for Rocket.rs

前端 未结 2 1646
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-22 18:35

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) t

2条回答
  •  清歌不尽
    2021-01-22 19:25

    Lambda Fairy's comment answered it for me.
    I put it all in the GET Handler:

    #[get("/")]
    fn get_handler<'a>() -> Response<'a> {
        let mut res = Response::new();
        res.set_status(Status::new(200, "No Content"));
        res.adjoin_header(ContentType::Plain);
        res.adjoin_raw_header("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
        res.adjoin_raw_header("Access-Control-Allow-Origin", "*");
        res.adjoin_raw_header("Access-Control-Allow-Credentials", "true");
        res.adjoin_raw_header("Access-Control-Allow-Headers", "Content-Type");
        res.set_sized_body(Cursor::new("Response")); 
        res
    

提交回复
热议问题