actix_web 提供的标头无效

问题描述 投票:0回答:2

我在运行基于 actix-web 的服务器时遇到此错误

ERROR actix_http::h1::dispatcher] stream error: Request parse error: Invalid Header provided

处理程序代码是这样的:

#[derive(Serialize, Deserialize)]
pub struct Data {
   some_data: String
};
async fn handler_post(
  request: HttpRequest,
  data: web::Json<Data>
) -> impl Responder {
  HttpResponse::OK()
     .json(ApiResponse {
        status: "success"
     })
}

发送的标头包括 Accept、Content-Type 和 User-Agent。我不知道如何让它发挥作用。顺便说一句,我正在使用 actix-web 4。

rust actix-web
2个回答
1
投票

我尝试了

actix
版本
4
的处理程序,没有遇到任何问题

src/main.rs

use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest};
use serde::{Serialize, Deserialize};

#[get("/")]
async fn hello() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[derive(Serialize, Deserialize)]
pub struct Data {
   some_data: String
}

#[derive(Serialize, Deserialize)]
pub struct ApiResponse<'a> {
   status: &'a str
}

#[post("/")]
async fn handler_post(
  request: HttpRequest,
  data: web::Json<Data>
) -> impl Responder {
  HttpResponse::Ok()
     .json(ApiResponse {
        status: "success"
     })
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(handler_post)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Cargo.toml

[package]
name = "..."
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
actix-web = "4"
serde = { version = "1.0", features = ["derive"] }

0
投票

我今天面临同样的问题,似乎是原始 utf8 问题。当请求包含中文单词时。会显示这个错误,移动中文单词后,工作正常。更多信息请点击这里:https://github.com/seanmonstar/httparse/issues/146

© www.soinside.com 2019 - 2024. All rights reserved.