How use cors in actix-web

please help for use Cors in actix-web

my code:

async  fn main() -> std::io::Result<()> {

HttpServer::new(||

        {
            let cors = Cors::default()
                .allowed_origin("http://127.0.0.1:8080/")

                .allowed_methods(vec!["GET", "POST"])
                .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
                .allowed_header(http::header::CONTENT_TYPE)
                .max_age(3600);
            App::new()


                .service(*******)
                .service(**********)
                .service(**********)
                .route("/hey", web::get().to(manual_hello))
        })
        .bind("127.0.0.1:8080")?
        .run()
        .await.unwrap();
Ok(())
}

Please follow our guidelines on code formatting.

1 Like

thanks

This code in postman is ok but with react js error cors ........

In addition, you need to add cors to your app:

      App::new()
           .wrap(cors)
  App::new().wrap(cors)
    |                             ^^^^ not found in this scope

Can you show your code? That error shouldn't happen. Maybe you removed the creation of the cors variable?

my code:

use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, http, middleware};
use serde::Deserialize;
use serde::Serialize;
use serde_json::Value;
use arangors::AqlQuery;
use actix_http::error::ParseError::Status;
use actix_http::http::header;
use actix_cors::Cors;

[actix_web::main]
async  fn main() -> std::io::Result<()> {

HttpServer::new(||

        {
            let cors = Cors::default()
                .allowed_origin("http://127.0.0.1:8080/")

                .allowed_methods(vec!["GET", "POST"])
                .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
                .allowed_header(http::header::CONTENT_TYPE)
                .max_age(3600);

            App::new()
                .wrap(cors)

                .service(hello)
                .service(echo)
                .service(adduser)
                .route("/hey", web::get().to(manual_hello))
        })
        .bind("127.0.0.1:8080")?
        .run()

        .await.unwrap();


    //---------------------------------------------------------------


    Ok(())
}
.wrap(cors)
   |                       ^^^^ the trait `Transform<actix_web::app_service::AppRouting, ServiceRequest>`
is not implemented for `Cors`

The only thing I can think of is a version mismatch. The latest version of actix-cors depends on actix-web 3 - make sure you're using that.

Here is an example for you.

use actix_cors::Cors;
use actix_web::{get, http, web, App, HttpRequest, HttpResponse, HttpServer};

#[get("/index.html")]
async fn index(req: HttpRequest) -> &'static str {
    "<p>Hello World!</p>"
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        let cors = Cors::default()
              .allowed_origin("https://www.rust-lang.org/")
              .allowed_origin_fn(|origin, _req_head| {
                  origin.as_bytes().ends_with(b".rust-lang.org")
              })
              .allowed_methods(vec!["GET", "POST"])
              .allowed_headers(vec![http::header::AUTHORIZATION, http::header::ACCEPT])
              .allowed_header(http::header::CONTENT_TYPE)
              .max_age(3600);

        App::new()
            .wrap(cors)
            .service(index)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await;

    Ok(())
}

My dependencies:

#tokio = "1.3.0"
tokio = { version = "1.3.0", features = ["full"] }
features = "0.10.0"
actix = "0.11.0-beta.3"
actix-http = "3.0.0-beta.4"
actix-web = "4.0.0-beta.3"
actix-cors = "0.5.4"
serde_json = "1.0.64"

http = "0.2.3"
#serde = "1.0.124"
serde_derive = "1.0.124"

serde = { version = "1.0.124", features = ["derive"] }

After use this code send error

 .wrap(cors)
    |                   ^^^^ the trait `Transform<actix_web::app_service::AppRouting, ServiceRequest>` is
not implemented for `Cors`

Yeah, that's the problem then - you're using actix-web 4 but actix-cors depends on actix-web 3. You will have to downgrade to Actix-web 3

thanks
:clap: :clap: :clap: :clap: :clap:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.