我完全是个生锈新手。我正在尝试用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.
有人能帮帮我吗?
发布于 2019-01-15 10:46:05
通过在URL中提供参数,您正在请求一个类似于GET
端点的POST
端点。尝试使用curl
执行以下操作
curl -d '{"id": 1, "title": "titre", "body": "corps"}' -H "Content-Type: application/json" -X POST http://localhost:8000/article/new
https://stackoverflow.com/questions/54196607
复制