mirror of
https://github.com/fafhrd91/actix-web
synced 2025-08-19 12:25:37 +02:00
Add protobuf support
This commit is contained in:
@@ -11,6 +11,7 @@ use http::{StatusCode, Version, HeaderMap, HttpTryFrom, Error as HttpError};
|
||||
use http::header::{self, HeaderName, HeaderValue};
|
||||
use serde_json;
|
||||
use serde::Serialize;
|
||||
use prost::Message;
|
||||
|
||||
use body::Body;
|
||||
use error::Error;
|
||||
@@ -508,6 +509,22 @@ impl HttpResponseBuilder {
|
||||
Ok(self.body(body)?)
|
||||
}
|
||||
|
||||
pub fn protobuf<T: Message>(&mut self, value: T) -> Result<HttpResponse, Error> {
|
||||
let mut body = Vec::new();
|
||||
value.encode(&mut body)?;
|
||||
|
||||
let contains = if let Some(parts) = parts(&mut self.response, &self.err) {
|
||||
parts.headers.contains_key(header::CONTENT_TYPE)
|
||||
} else {
|
||||
true
|
||||
};
|
||||
if !contains {
|
||||
self.header(header::CONTENT_TYPE, "application/protobuf");
|
||||
}
|
||||
|
||||
Ok(self.body(body)?)
|
||||
}
|
||||
|
||||
/// Set an empty body and generate `HttpResponse`
|
||||
///
|
||||
/// `HttpResponseBuilder` can not be used after this call.
|
||||
|
Reference in New Issue
Block a user