How works Json<T> (Form data does not have form content type)

Viewed 512

I'm totaly new to rust. I'm trying to create a very simple API with rocket. I have the folowing route that dosn't work and I don't know why.

#![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();
}

When I send the request I have the folowing :

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.

Can someone help me ?

1 Answers

You are requesting a POST endpoint like a GET endpoint by providing the parameters in the URL. Try the following with curl:

curl -d '{"id": 1, "title": "titre", "body": "corps"}' -H "Content-Type: application/json" -X POST http://localhost:8000/article/new
Related