mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-13 19:27:53 +01:00
A few small refactorings (#655)
* Simplify json content-type check * Import HeaderMap from http instead of from headers The headers crate is an optional dependency and its HeaderMap re-export is `#[doc(hidden)]`. * Use headers re-export in axum in examples
This commit is contained in:
parent
f4716084a7
commit
0a399ed0fa
5 changed files with 6 additions and 6 deletions
|
@ -40,12 +40,12 @@ Some commonly used extractors are:
|
|||
use axum::{
|
||||
extract::{Json, TypedHeader, Path, Extension, Query},
|
||||
routing::post,
|
||||
headers::UserAgent,
|
||||
http::{Request, header::HeaderMap},
|
||||
body::{Bytes, Body},
|
||||
Router,
|
||||
};
|
||||
use serde_json::Value;
|
||||
use headers::UserAgent;
|
||||
use std::collections::HashMap;
|
||||
|
||||
// `Path` gives you the path parameters and deserializes them. See its docs for
|
||||
|
@ -148,10 +148,10 @@ individual headers first:
|
|||
use axum::{
|
||||
extract::TypedHeader,
|
||||
routing::get,
|
||||
headers::UserAgent,
|
||||
http::header::HeaderMap,
|
||||
Router,
|
||||
};
|
||||
use headers::UserAgent;
|
||||
|
||||
async fn handler(
|
||||
TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
|
|
|
@ -14,10 +14,10 @@ use std::ops::Deref;
|
|||
/// ```rust,no_run
|
||||
/// use axum::{
|
||||
/// extract::TypedHeader,
|
||||
/// headers::UserAgent,
|
||||
/// routing::get,
|
||||
/// Router,
|
||||
/// };
|
||||
/// use headers::UserAgent;
|
||||
///
|
||||
/// async fn users_teams_show(
|
||||
/// TypedHeader(user_agent): TypedHeader<UserAgent>,
|
||||
|
|
|
@ -132,7 +132,7 @@ fn json_content_type<B>(req: &RequestParts<B>) -> Result<bool, HeadersAlreadyExt
|
|||
};
|
||||
|
||||
let is_json_content_type = mime.type_() == "application"
|
||||
&& (mime.subtype() == "json" || mime.suffix().filter(|name| *name == "json").is_some());
|
||||
&& (mime.subtype() == "json" || mime.suffix().map_or(false, |name| name == "json"));
|
||||
|
||||
Ok(is_json_content_type)
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use tower::ServiceExt;
|
|||
|
||||
mod for_handlers {
|
||||
use super::*;
|
||||
use headers::HeaderMap;
|
||||
use http::HeaderMap;
|
||||
|
||||
#[tokio::test]
|
||||
async fn get_handles_head() {
|
||||
|
|
|
@ -9,12 +9,12 @@
|
|||
use axum::{
|
||||
async_trait,
|
||||
extract::{FromRequest, RequestParts, TypedHeader},
|
||||
headers::{authorization::Bearer, Authorization},
|
||||
http::StatusCode,
|
||||
response::{IntoResponse, Response},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use headers::{authorization::Bearer, Authorization};
|
||||
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation};
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
|
Loading…
Add table
Reference in a new issue