From 0a0839cc3cca7883d64e2529f9098390e45d9b83 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Sat, 18 Feb 2023 21:41:23 -0800 Subject: [PATCH] Content type highlighting --- src-tauri/src/commands.rs | 15 ++++++++++++--- src-web/App.tsx | 6 +++++- src-web/components/Editor/Editor.tsx | 4 ++-- src-web/hooks/useCodemirror.ts | 17 +++++++++++------ 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src-tauri/src/commands.rs b/src-tauri/src/commands.rs index 657da338..28010031 100644 --- a/src-tauri/src/commands.rs +++ b/src-tauri/src/commands.rs @@ -1,6 +1,7 @@ use http::header::{HeaderName, USER_AGENT}; use http::{HeaderMap, HeaderValue, Method}; use reqwest::redirect::Policy; +use std::collections::HashMap; use tauri::{AppHandle, Wry}; #[derive(serde::Serialize)] @@ -11,6 +12,7 @@ pub struct CustomResponse { method: String, elapsed: u128, elapsed2: u128, + headers: HashMap, } #[tauri::command] @@ -59,8 +61,14 @@ pub async fn send_request( match resp { Ok(v) => { - let url2 = v.url().to_string(); + let url = v.url().to_string(); let status = v.status().to_string(); + let method = method.to_string(); + let headers = v + .headers() + .iter() + .map(|(k, v)| (k.as_str().to_string(), v.to_str().unwrap().to_string())) + .collect::>(); let body = v.text().await.unwrap(); let elapsed2 = start.elapsed().as_millis(); Ok(CustomResponse { @@ -68,8 +76,9 @@ pub async fn send_request( body, elapsed, elapsed2, - method: method.to_string(), - url: url2, + method, + url, + headers, }) } Err(e) => { diff --git a/src-web/App.tsx b/src-web/App.tsx index 46551323..93ba0296 100644 --- a/src-web/App.tsx +++ b/src-web/App.tsx @@ -15,6 +15,7 @@ interface Response { status: string; elapsed: number; elapsed2: number; + headers: Record; } function App() { @@ -27,6 +28,7 @@ function App() { e.preventDefault(); setLoading(true); const resp = (await invoke('send_request', { method, url })) as Response; + console.log('RESP', resp); if (resp.body.includes('')) { resp.body = resp.body.replace(//gi, ``); } @@ -34,6 +36,8 @@ function App() { setResponseBody(resp); } + const contentType = responseBody?.headers['content-type']?.split(';')[0] ?? 'text/plain'; + return ( <> @@ -79,7 +83,7 @@ function App() { {responseBody?.elapsed2}ms - +