Access-Control-Allow-Origin Actix-CORS

I have Rust-code running on 127.0.0.1:8080 and want to make a GET/POST request to it from rustsite.local (another domain).

However, that is not possible with CORS restriction. I find nowhere in the Actix-CORS documentation on how to allow Access-Control-Allow-Origin so specific domains can make requests to 127.0.0.1:8080.

Please try this:

use actix_cors::Cors;
use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn hello_world() -> impl Responder {
    HttpResponse::Ok().body("Hello, World!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().wrap(Cors::new()
        .allowed_origin("http://localhost:8080")
        .finish()
    ).route("/hello", web::get().to(hello_world)))
        .bind("127.0.0.1:8081")?
        .run()
        .await
}

Dependencies in Cargo.toml:

actix-cors = "0.3.0"
actix-web = "3"
2 Likes

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.