Add support for multiple HTTP methods in streamed_fetch and fetchNative

This commit is contained in:
Kwaroran
2024-12-21 01:26:01 +09:00
parent 45e8ca5ed6
commit 9d0402b7f5
2 changed files with 51 additions and 7 deletions

View File

@@ -333,6 +333,7 @@ async fn streamed_fetch(
headers: String,
body: String,
app: AppHandle,
method: String,
) -> String {
//parse headers
let headers_json: Value = match serde_json::from_str(&headers) {
@@ -357,14 +358,51 @@ async fn streamed_fetch(
return format!(r#"{{"success":false,"body":"Invalid header JSON"}}"#);
}
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
let client = reqwest::Client::new();
let response = client
let builder: reqwest::RequestBuilder;
if method == "POST" {
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
builder = client
.post(&url)
.headers(headers)
.timeout(Duration::from_secs(240))
.body(body_decoded)
}
else if method == "GET" {
builder = client
.get(&url)
.headers(headers)
.timeout(Duration::from_secs(240));
}
else if method == "PUT" {
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
builder = client
.put(&url)
.headers(headers)
.timeout(Duration::from_secs(240))
.body(body_decoded)
}
else if method == "DELETE" {
let body_decoded = general_purpose::STANDARD.decode(body.as_bytes()).unwrap();
builder = client
.delete(&url)
.headers(headers)
.timeout(Duration::from_secs(240))
.body(body_decoded)
}
else {
return format!(r#"{{"success":false, body:"Invalid method"}}"#);
}
let response = builder
.send()
.await;