Skip to content

Mapping response based on query parameter #1456

Answered by davidpdrsn
sagacity asked this question in Q&A
Discussion options

You must be logged in to vote

You can do something like this

use axum::{
    async_trait,
    extract::{FromRequestParts, Query},
    http::request::Parts,
    response::Response,
};
use serde::{Deserialize, Serialize};

async fn handler(format: Format) -> Response {
    #[derive(Serialize)]
    struct Data {
        value: String,
    }

    format.render(Data {
        value: "foo".to_owned(),
    })
}

enum Format {
    Json,
    Xml,
}

impl Format {
    fn render<T>(self, data: T) -> Response
    where
        T: Serialize,
    {
        match self {
            Format::Json => {
                todo!("render json...")
            }
            Format::Xml => {
                todo!("render xml...")
            }

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@sagacity
Comment options

@davidpdrsn
Comment options

@sagacity
Comment options

Answer selected by sagacity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants