未来<项 = http响应,错误 = actix_web::错误> - 找不到"项"和"错误"关联类型?



我正在尝试使用Tokio, requestwest和Actix-web,并创建一个连接到SendGrid的API来发送简单的电子邮件。

我卡住了一个错误:

error[E0220]: associated type `Item` not found for `std::future::Future`
--> srcmain.rs:20:79
|
20 |     request: web::Json<EmailRequest>,) -> impl tokio::macros::support::Future<Item = HttpResponse, Error = actix_web::Error> {
|                                                                               ^^^^ associated type `Item` not found
error[E0220]: associated type `Error` not found for `std::future::Future`
--> srcmain.rs:20:100
|
20 |     request: web::Json<EmailRequest>,) -> impl tokio::macros::support::Future<Item = HttpResponse, Error = actix_web::Error> {
|                                                                                                    ^^^^^ associated type `Error` not found

我的理解可能有限,但如果Future来自tokio,为什么stduture::Future没有显示Item和error的错误?

下面是我的代码:
extern crate actix_web;
extern crate reqwest;
extern crate tokio;
extern crate serde_json;
extern crate serde;
use actix_web::{web::{self, Json}, App, HttpResponse, HttpServer, http};
use reqwest::Client;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
struct EmailRequest {
to: String,
subject: String,
text: String,
}
fn send_email(request: Json<EmailRequest>) -> impl tokio::macros::support::Future<Item=HttpResponse, Error=actix_web::Error> {

// Set up the request body
let body = serde_json::json!({
"personalizations": [{
"to": [{ "email": request.to }],
"subject": request.subject,
}],
"from": { "email": "sender@example.com" },
"content": [{ "type": "text/plain", "value": request.text }],
});
// Create an HTTP client
let client = Client::new();
// Send the request
client
.post("https://api.sendgrid.com/v3/mail/send")
.header("Authorization", "Bearer YOUR_API_KEY")
.header("Content-Type", "application/json")
.send_json(&body)
.map_err(actix_web::Error::from)
.and_then(|response| {
if response.status() == http::StatusCode::ACCEPTED {
Ok(HttpResponse::Ok().body("Email sent successfully!".to_string()))
} else {
Ok(HttpResponse::InternalServerError().body("Error sending email!".to_string()))
}
})
}
fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/api/send_email", web::post().to(send_email)))
.bind("localhost:8080")?
.run()
}

仅供参考,我的货物依赖是:

[dependencies]
reqwest = "0.11.13"
tokio = { version = "1", features = ["full"] }
actix-web = "4.2.1"
serde = { version = "1.0.152", features = ["derive"]}
serde_json = "1.0.91"
[features]
default = ["tokio/full"]

任何帮助都将非常感激。

我尝试使用各种各样的板条箱,但每当我使用一个未来我得到一个问题与相关的类型。我确实使用了Rocket,但是结果有问题。

我想知道问题是否与客户端响应不匹配的函数输出有关。

我做了以下更改,效果很好。我删除了Future并添加了Async,我不得不做一些其他的改变,因为它通过一些更多的问题,但以下工作。谢谢你帮助一个初学者。

extern crate actix_web;
extern crate reqwest;
extern crate serde;
extern crate serde_json;
extern crate tokio;

use actix_web::{
http,
web::{self, Json},
App, HttpResponse, HttpServer,
};
use reqwest::Client;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct EmailRequest {
to: String,
subject: String,
text: String,
}

async fn send_email(request: Json<EmailRequest>) -> Result<HttpResponse, actix_web::Error> {
// Set up the request body
let body = serde_json::json!({
"personalizations": [{
"to": [{ "email": request.to }],
"subject": request.subject,
}],
"from": { "email": "sender@example.com" },
"content": [{ "type": "text/plain", "value": request.text }],
});

// Create an HTTP client
let client = Client::new();

// Send the request
let response = client
.post("https://api.sendgrid.com/v3/mail/send")
.header("Authorization", "Bearer YOUR_API_KEY")
.header("Content-Type", "application/json")
.json(&body)
.send()
.await
.map_err(reqwest::Error::from).unwrap();

if response.status() == http::StatusCode::ACCEPTED {
Ok(HttpResponse::Ok().body("Email sent successfully!".to_string()))
} else {
Ok(HttpResponse::InternalServerError().body("Error sending email!".to_string()))
}
}

#[tokio::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| App::new().route("/api/send_email", web::post().to(send_email)))
.bind("localhost:8080")?
.run()
.await
}

相关内容

  • 没有找到相关文章

最新更新