How do I respond from a Rocket handler with content type application/hal+json?

636 views Asked by At

I have a project written in Rocket with the endpoint /foo that returns data in application/json. I'm using rocket, rocket_codegen, serde, and serde_json.

#[get("/foo")]
fn foo() -> Json {
    Json(json!({
        "foo": 1
    }))
}

This works nicely, but I need to respond with application/hal+json so I guess I need to write my own response, and I have failed. How do I return my JSON with the Content-Type application/hal+json?

1

There are 1 answers

0
nsg On

I got some help over at the projects chat and the solution was:

#[get("/foo")]
fn foo() -> Content<Json> {
    let r = json!({
        "foo": 1
    })
    Content(ContentType::new("application", "hal+json"), Json(r))
}