3

我对生锈完全陌生。我正在尝试用 Rocket 创建一个非常简单的 API。我的以下路线不起作用,我不知道为什么。

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use rocket_contrib::json::Json;
use serde::{Serialize, Deserialize};
use serde_json::Result as JsonResult;

#[derive(Serialize, Deserialize)]
struct Article {
    id: usize,
    title: String,
    body: String,
}

#[post("/new", format = "application/json", data = "<article>")]
fn create_article (article: Json<Article>) -> JsonResult<()> {
    println!("Article is: id:{}, title:{}, body:{}", article.id, article.title, article.body);
    Ok(())
}

fn main() {
    rocket::ignite()
    .mount("/article", routes![create_article])
    .launch();
}

当我发送请求时,我有以下内容:

POST /article/new?id=1&title=titre&body=corps application/json:
    => Matched: POST /article/new (create_article)
    => Warning: Form data does not have form content type.
    => Outcome: Forward
    => Error: No matching routes for POST /article/new?id=1&title=titre&body=corps application/json.
    => Warning: Responding with 404 Not Found catcher.
    => Response succeeded.

有人能帮我吗 ?

4

1 回答 1

2

您通过在 URL 中提供参数来请求一个POST端点,如端点。GET尝试以下操作curl

curl -d '{"id": 1, "title": "titre", "body": "corps"}' -H "Content-Type: application/json" -X POST http://localhost:8000/article/new
于 2019-01-15T10:46:04.507 回答