@@ -34,6 +34,17 @@ pub async fn http_request(
3434 base : & str ,
3535 path : & str ,
3636 body : & [ u8 ] ,
37+ ) -> Result < ( u16 , Vec < u8 > ) > {
38+ http_request_with_headers ( method, base, path, body, & [ ] ) . await
39+ }
40+
41+ /// Same as [`http_request`], with extra request headers (e.g. `Authorization`).
42+ pub async fn http_request_with_headers (
43+ method : & str ,
44+ base : & str ,
45+ path : & str ,
46+ body : & [ u8 ] ,
47+ headers : & [ ( & str , & str ) ] ,
3748) -> Result < ( u16 , Vec < u8 > ) > {
3849 debug ! ( "Sending HTTP request to {base}, path={path}" ) ;
3950 let mut response = if let Some ( uds) = base. strip_prefix ( "unix:" ) {
@@ -44,24 +55,29 @@ pub async fn http_request(
4455 } ;
4556 let client: Client < UnixConnector , Full < Bytes > > = Client :: unix ( ) ;
4657 let unix_uri: hyper:: Uri = Uri :: new ( uds, & path) . into ( ) ;
47- let req = Request :: builder ( )
48- . method ( method)
49- . uri ( unix_uri)
50- . body ( Full :: new ( Bytes :: copy_from_slice ( body) ) ) ?;
58+ let mut builder = Request :: builder ( ) . method ( method) . uri ( unix_uri) ;
59+ for ( name, value) in headers {
60+ builder = builder. header ( * name, * value) ;
61+ }
62+ let req = builder. body ( Full :: new ( Bytes :: copy_from_slice ( body) ) ) ?;
5163 client. request ( req) . await ?
5264 } else if base. starts_with ( "vsock:" ) {
5365 let client = Client :: vsock ( ) ;
5466 let uri = mk_url ( base, path) . parse :: < hyper:: Uri > ( ) ?;
55- let req = Request :: builder ( )
56- . method ( method)
57- . uri ( uri)
58- . body ( Full :: new ( Bytes :: copy_from_slice ( body) ) ) ?;
67+ let mut builder = Request :: builder ( ) . method ( method) . uri ( uri) ;
68+ for ( name, value) in headers {
69+ builder = builder. header ( * name, * value) ;
70+ }
71+ let req = builder. body ( Full :: new ( Bytes :: copy_from_slice ( body) ) ) ?;
5972 client. request ( req) . await ?
6073 } else {
6174 let uri = mk_url ( base, path) ;
6275 let client = reqwest:: Client :: builder ( ) . build ( ) ?;
6376 let method = reqwest:: Method :: from_bytes ( method. as_bytes ( ) ) ?;
6477 let mut request = client. request ( method, uri) ;
78+ for ( name, value) in headers {
79+ request = request. header ( * name, * value) ;
80+ }
6581 if !body. is_empty ( ) {
6682 request = request. body ( body. to_vec ( ) ) ;
6783 }
0 commit comments