使用curl向Actix服务器发出POST请求失败并显示“400 Bad Request”

AMP*_*035 3 curl rust amazon-ses actix-web

我正在尝试构建一个 actix 服务器,用于actix-web = "3.3.2"向 AWS SES 发送 POST 请求,该请求将向路由正文中提供的地址发送电子邮件。我创建了一个名为 的路由,signup它返回带有以下curl请求的 400 响应:

curl -i POST -d "name=test_name&email=testemail@test.com" 127.0.0.1:8000/signup -v

我还尝试使用以下方法发送argjson对象-d

curl -i POST -d '{"name": "test_name", "email": "testemail@test.com"}' 127.0.0.1:8000/signup -v

两者都回应:

curl: (6) Could not resolve host: POST
* Expire in 0 ms for 6 (transfer 0x5c7286c42fb0)
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Expire in 200 ms for 4 (transfer 0x5c7286c42fb0)
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#1)
> POST /signup HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.0
> Accept: */*
> Content-Length: 42
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 42 out of 42 bytes
< HTTP/1.1 400 Bad Request
HTTP/1.1 400 Bad Request
< content-length: 0
content-length: 0

< 
* Connection #1 to host 127.0.0.1 left intact

Run Code Online (Sandbox Code Playgroud)

在我的main.rs文件中,我有以下内容创建在端口 8000 上运行的 actix 服务器。

#[actix_web::main]
async fn main() -> Result<(), StdErr> {
    env_logger::init();
    HttpServer::new(move || {
        actix_web::App::new()
            .wrap(Logger::default())
            .service(signup)
    })
        .bind(("127.0.0.1", 8000))?
        .run()
        .await?;

    Ok(())
}

Run Code Online (Sandbox Code Playgroud)

我的服务signup所在的位置src/signup包含一个从我的文件中mod.rs公开的文件,我在其中编写了以下内容:pub mod routesroutes.rs

#[post("/signup")]
pub async fn signup(body: actix_web::web::Json<SignupBody>) -> impl Responder {
    let name = &body.name;
    let email = &body.email;

    let message = send_message(name.to_string(), email.to_string()) 
        .await;

    match message {
        Ok(()) => {
            web::Json(
                SignupResp::Success(Success{ message: "Email Sent Successfully".into() })
            )
        }

        Err(e) => {
            web::Json(
                SignupResp::ErrorResp(ErrorResp{ message: format!("{}", e) })  

            )
        }
    }
}

async fn send_message(name: String, email: String) -> Result<(), Box<dyn std::error::Error>> {
    let ses_client = SesClient::new(rusoto_core::Region::UsEast1);
    let from = "Test <test@test.com>";
    let to = format!("{}, <{}>", name, email); 
    let subject = "Signup";
    let body = "<h1>User Signup</h1>".to_string();

    send_email_ses(&ses_client, from, &to, subject, body).await
}

async fn send_email_ses(
    ses_client: &SesClient,
    from: &str,
    to: &str,
    subject: &str,
    body: String,
) -> Result<(), Box<dyn std::error::Error>> {
    let email = Message::builder()
        .from(from.parse()?)
        .to(to.parse()?)
        .subject(subject)
        .body(body.to_string())?;

    let raw_email = email.formatted();

    let ses_request = SendRawEmailRequest {
        raw_message: RawMessage {
            data: base64::encode(raw_email).into(),
        },
        ..Default::default()
    };

    ses_client.send_raw_email(ses_request).await?;

    Ok(())
}

Run Code Online (Sandbox Code Playgroud)

我是 actix 的新手,想知道是否有人可以验证我的请求是否curl格式错误,或者我是否在我的 actix 服务器中弄乱了某些内容。任何帮助将不胜感激。

moy*_*010 7

您的请求被拒绝,因为它未指定内容类型。

为此,您必须添加Content-Type标头。在 cURL 中你可以这样做:

-H "Content-Type: application/json"