Rust rocket post json -> json example

  1. I am trying to use rust + rocket to build a REST API that handles just POST and does JSON -> JSON

  2. I have written the following code so far:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

use std::path::PathBuf;

#[post("/api")]
fn root_api() -> &'static str {
    handler()
}

#[post("/api/<path..>")]
fn child_api(path: PathBuf) -> &'static str {
    handler()
}

fn handler() -> &'static str {
    "Hello, world!"
}

pub fn main() {
    rocket::ignite().mount("/", routes![root_api, child_api]).launch();
}
  1. The code works fine when I run:
curl -X POST http://localhost:8000/api -d "@data.txt"

  1. What I need help with now (and what I can’t find docs for) is: how do I write a handler that does JSON -> JSON ? I want a handler where it uses serde to get the input field as a JSON object, then returns a JSON object.

  2. Any idea where there is sample code for this? (The rocket docs seems to be focused on routing.)

Thanks!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.