@@ -34,6 +34,20 @@ async fn create_surface<'a: 'n>(_: impl Ctx, editor: &'a WasmEditorApi) -> Arc<W
3434 Arc :: new ( editor. application_io . as_ref ( ) . unwrap ( ) . create_window ( ) )
3535}
3636
37+ fn parse_headers ( headers : & str ) -> reqwest:: header:: HeaderMap {
38+ use reqwest:: header:: { HeaderMap , HeaderName , HeaderValue } ;
39+
40+ let mut header_map = HeaderMap :: new ( ) ;
41+ for line in headers. lines ( ) {
42+ if let Some ( ( key, value) ) = line. split_once ( ':' ) {
43+ let Ok ( header_name) = HeaderName :: from_bytes ( key. trim ( ) . as_bytes ( ) ) else { continue } ;
44+ let Ok ( header_value) = HeaderValue :: from_str ( value. trim ( ) ) else { continue } ;
45+ header_map. insert ( header_name, header_value) ;
46+ }
47+ }
48+ header_map
49+ }
50+
3751/// Sends an HTTP GET request to a specified URL and optionally waits for the response (unless discarded) which is output as a string.
3852#[ node_macro:: node( category( "Web Request" ) ) ]
3953async fn get_request (
@@ -44,32 +58,26 @@ async fn get_request(
4458 url : String ,
4559 /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph.
4660 discard_result : bool ,
61+ #[ widget( ParsedWidgetOverride :: Custom = "text_area" ) ] headers : String ,
4762) -> String {
48- #[ cfg( target_family = "wasm" ) ]
49- {
50- if discard_result {
51- wasm_bindgen_futures:: spawn_local ( async move {
52- let _ = reqwest:: get ( url) . await ;
53- } ) ;
54- return String :: new ( ) ;
55- }
56- }
57- #[ cfg( not( target_family = "wasm" ) ) ]
58- {
59- #[ cfg( feature = "tokio" ) ]
60- if discard_result {
61- tokio:: spawn ( async move {
62- let _ = reqwest:: get ( url) . await ;
63- } ) ;
64- return String :: new ( ) ;
65- }
66- #[ cfg( not( feature = "tokio" ) ) ]
67- if discard_result {
68- return String :: new ( ) ;
69- }
63+ let header_map = parse_headers ( & headers) ;
64+ let request = reqwest:: Client :: new ( ) . get ( url) . headers ( header_map) ;
65+
66+ if discard_result {
67+ #[ cfg( target_family = "wasm" ) ]
68+ wasm_bindgen_futures:: spawn_local ( async move {
69+ let _ = request. send ( ) . await ;
70+ } ) ;
71+ #[ cfg( all( not( target_family = "wasm" ) , feature = "tokio" ) ) ]
72+ tokio:: spawn ( async move {
73+ let _ = request. send ( ) . await ;
74+ } ) ;
75+ return String :: new ( ) ;
7076 }
7177
72- let Ok ( response) = reqwest:: get ( url) . await else { return String :: new ( ) } ;
78+ let Ok ( response) = request. send ( ) . await else {
79+ return String :: new ( ) ;
80+ } ;
7381 response. text ( ) . await . ok ( ) . unwrap_or_default ( )
7482}
7583
@@ -85,34 +93,25 @@ async fn post_request(
8593 body : Vec < u8 > ,
8694 /// Makes the request run in the background without waiting on a response. This is useful for triggering webhooks without blocking the continued execution of the graph.
8795 discard_result : bool ,
96+ #[ widget( ParsedWidgetOverride :: Custom = "text_area" ) ] headers : String ,
8897) -> String {
89- #[ cfg( target_family = "wasm" ) ]
90- {
91- if discard_result {
92- wasm_bindgen_futures:: spawn_local ( async move {
93- let _ = reqwest:: Client :: new ( ) . post ( url) . body ( body) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await ;
94- } ) ;
95- return String :: new ( ) ;
96- }
97- }
98- #[ cfg( not( target_family = "wasm" ) ) ]
99- {
100- #[ cfg( feature = "tokio" ) ]
101- if discard_result {
102- let url = url. clone ( ) ;
103- let body = body. clone ( ) ;
104- tokio:: spawn ( async move {
105- let _ = reqwest:: Client :: new ( ) . post ( url) . body ( body) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await ;
106- } ) ;
107- return String :: new ( ) ;
108- }
109- #[ cfg( not( feature = "tokio" ) ) ]
110- if discard_result {
111- return String :: new ( ) ;
112- }
98+ let mut header_map = parse_headers ( & headers) ;
99+ header_map. insert ( "Content-Type" , "application/octet-stream" . parse ( ) . unwrap ( ) ) ;
100+ let request = reqwest:: Client :: new ( ) . post ( url) . body ( body) . headers ( header_map) ;
101+
102+ if discard_result {
103+ #[ cfg( target_family = "wasm" ) ]
104+ wasm_bindgen_futures:: spawn_local ( async move {
105+ let _ = request. send ( ) . await ;
106+ } ) ;
107+ #[ cfg( all( not( target_family = "wasm" ) , feature = "tokio" ) ) ]
108+ tokio:: spawn ( async move {
109+ let _ = request. send ( ) . await ;
110+ } ) ;
111+ return String :: new ( ) ;
113112 }
114113
115- let Ok ( response) = reqwest :: Client :: new ( ) . post ( url ) . body ( body ) . header ( "Content-Type" , "application/octet-stream" ) . send ( ) . await else {
114+ let Ok ( response) = request . send ( ) . await else {
116115 return String :: new ( ) ;
117116 } ;
118117 response. text ( ) . await . ok ( ) . unwrap_or_default ( )
0 commit comments