小编Alc*_*ist的帖子

Actix Web 不处理 post 请求?

因此,我尝试创建一个基本的 actix-web 应用程序,它允许我创建一个非常基本的博客系统。它正在处理我的 GET 请求,但不处理我的 POST 请求。

主要.rs:

use actix_web::{HttpServer, App, web};
use sqlx::postgres::PgPool;
use dotenv::dotenv;

mod posts;
mod database;

#[actix_web::main]
pub async fn main() -> std::io::Result<()>{
    dotenv().ok();

    let pool = PgPool::connect(&dotenv::var("DATABASE_URL").unwrap()).await.unwrap();

    HttpServer::new(move || {
        // The scope for all post services
        let posts = web::scope("/posts").service(posts::get_all_posts).service(posts::create_post);

        App::new()
            .data(pool.clone())
            .service(web::scope("/api").service(posts))
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}
Run Code Online (Sandbox Code Playgroud)

帖子/routes.rs:

use super::*;
use database::{NewPost, model::Post};
use actix_web::{get, post, Responder, HttpResponse};

#[get("/")]
pub async fn get_all_posts(pool: web::Data<PgPool>) -> impl Responder {
    println!("New GET request for …
Run Code Online (Sandbox Code Playgroud)

rust actix-web

6
推荐指数
1
解决办法
5199
查看次数

标签 统计

actix-web ×1

rust ×1