plugins-workspace icon indicating copy to clipboard operation
plugins-workspace copied to clipboard

[bug] Mobile http request sending error

Open gato25 opened this issue 1 year ago • 4 comments

Describe the bug

when i send this from desktop its working fine

const response = await fetch(
    "https://blablabla.fly.dev/sql",
    {
        method: "POST",
        headers: {
            Accept: "application/json",
            "Content-Type": "text/plain",
        },
        body: sqlQuery,
    },
)

But when i call it on android it gives me "error sending request for url".

default capability json

{
  "$schema": "../gen/schemas/desktop-schema.json",
  "identifier": "default",
  "description": "Capability for the main window",
  "windows": ["main"],
  "permissions": [
    "path:default",
    "event:default",
    "window:default",
    "app:default",
    "image:default",
    "resources:default",
    "menu:default",
    "tray:default",
    "shell:allow-open",
    {
      "identifier": "http:default",
      "allow": [{ "url": "https://*" }],
      "deny": [{ "url": "https://private.tauri.app" }]
    }
  ]
}

Reproduction

No response

Expected behavior

No response

Full tauri info output

[✔] Environment
    - OS: Windows 10.0.22621 X64
    ✔ WebView2: 124.0.2478.97
    ✔ MSVC: Visual Studio Build Tools 2022
    ✔ rustc: 1.78.0 (9b00956e5 2024-04-29)
    ✔ cargo: 1.78.0 (54d8815d0 2024-03-26)
    ✔ rustup: 1.27.0 (bbb9276d2 2024-03-08)
    ✔ Rust toolchain: stable-x86_64-pc-windows-msvc (default)
    - node: 18.16.1
    - pnpm: 9.0.4
    - npm: 9.5.1
    - bun: 1.1.7

[-] Packages
    - tauri [RUST]: 2.0.0-beta.20
    - tauri-build [RUST]: 2.0.0-beta.16
    - wry [RUST]: 0.40.0
    - tao [RUST]: 0.28.0
    - @tauri-apps/api [NPM]: 2.0.0-beta.12
    - @tauri-apps/cli [NPM]: 2.0.0-beta.18

[-] App
    - build-type: bundle
    - CSP: unset
    - frontendDist: ../dist
    - devUrl: http://localhost:1420/
    - framework: Svelte
    - bundler: Vite

Stack trace

No response

Additional context

No response

gato25 avatar May 24 '24 09:05 gato25

I also encountered this problem, is there a solution now

zhaihaozhen avatar Nov 25 '24 07:11 zhaihaozhen

I got the same issue.

import { fetch } from "@tauri-apps/plugin-http";

const response = await fetch(chat.url, {
  method: "POST",
  headers: headers,
  body: JSON.stringify(body),
});

error sending request for url (...) Image

[-] Packages
"dependencies": {
  "@element-plus/icons-vue": "^2.3.1",
  "@tauri-apps/api": "^2.4.0",
  "@tauri-apps/plugin-fs": "^2.2.1",
  "@tauri-apps/plugin-http": "^2",
  "@tauri-apps/plugin-opener": "^2",
  "element-plus": "^2.9.3",
  "js-yaml": "^4.1.0",
  "less-loader": "^12.2.0",
  "marked": "^15.0.7",
  "vue": "^3.5.13",
  "xlsx": "^0.18.5"
},

[-] Cargo.toml
[dependencies]
tauri = { version = "2", features = [] }
tauri-plugin-opener = "2"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
tauri-plugin-fs = "2.0.0"
tauri-plugin-http = { version = "2.0.0-beta", features = ["rustls-tls"] }
reqwest = { version = "0.11", features = ["blocking", "json"] }
base64 = "0.13.0"



ecojust avatar Apr 08 '25 06:04 ecojust

You might want to change the idea of sending a request through rust

  1. Use rust to define a request event
  2. Call the rust backend function at the corresponding time
  3. Accept data from custom functions
  4. Finally finish the rendering

---Original--- From: @.> Date: Tue, Apr 8, 2025 14:33 PM To: @.>; Cc: @.@.>; Subject: Re: [tauri-apps/plugins-workspace] [bug] Mobile http request sendingerror (Issue #1370)

I got the same issue. import { fetch } from @./plugin-http"; const response = await fetch(chat.url, { method: "POST", headers: headers, body: JSON.stringify(body), });
error sending request for url (...) image.png (view on web) [-] Packages "dependencies": { @.
/icons-vue": "^2.3.1", @./api": "^2.4.0", @./plugin-fs": "^2.2.1", @./plugin-http": "^2", @./plugin-opener": "^2", "element-plus": "^2.9.3", "js-yaml": "^4.1.0", "less-loader": "^12.2.0", "marked": "^15.0.7", "vue": "^3.5.13", "xlsx": "^0.18.5" }, [-] Cargo.toml [dependencies] tauri = { version = "2", features = [] } tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" tauri-plugin-fs = "2.0.0" tauri-plugin-http = { version = "2.0.0-beta", features = ["rustls-tls"] } reqwest = { version = "0.11", features = ["blocking", "json"] } base64 = "0.13.0" — Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***> ecojust left a comment (tauri-apps/plugins-workspace#1370)

I got the same issue. import { fetch } from @./plugin-http"; const response = await fetch(chat.url, { method: "POST", headers: headers, body: JSON.stringify(body), });
error sending request for url (...) image.png (view on web) [-] Packages "dependencies": { @.
/icons-vue": "^2.3.1", @./api": "^2.4.0", @./plugin-fs": "^2.2.1", @./plugin-http": "^2", @./plugin-opener": "^2", "element-plus": "^2.9.3", "js-yaml": "^4.1.0", "less-loader": "^12.2.0", "marked": "^15.0.7", "vue": "^3.5.13", "xlsx": "^0.18.5" }, [-] Cargo.toml [dependencies] tauri = { version = "2", features = [] } tauri-plugin-opener = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" tauri-plugin-fs = "2.0.0" tauri-plugin-http = { version = "2.0.0-beta", features = ["rustls-tls"] } reqwest = { version = "0.11", features = ["blocking", "json"] } base64 = "0.13.0"
— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you commented.Message ID: @.***>

zhaihaozhen avatar Apr 08 '25 06:04 zhaihaozhen

You might want to change the idea of sending a request through rust

  1. Use rust to define a request event
  2. Call the rust backend function at the corresponding time
  3. Accept data from custom functions
  4. Finally finish the rendering

I sending the request through rust, it also cannot work.

use reqwest::blocking::{Client, Response};
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, AUTHORIZATION};
use reqwest::Error;


#[tauri::command]
pub fn fetch_tauri(url: &str, method: &str, body: Option<&str>) -> String {
    let mut headers = HeaderMap::new();
    let client = Client::builder()
        .danger_accept_invalid_certs(true)
        .default_headers(headers)
        .build()
        .unwrap();
    // 修改请求处理逻辑,支持携带 body 数据
    let res = match method {
        "GET" => client.get(url).send(),
        "POST" => {
            let request = client.post(url);
            match body {
                Some(data) => request.body(data.to_string()).send(),
                None => request.send(),
            }
        }
        "PUT" => {
            let request = client.put(url);
            match body {
                Some(data) => request.body(data.to_string()).send(),
                None => request.send(),
            }
        }
        "DELETE" => client.delete(url).send(),
        _ => return String::from("Invalid HTTP method"),
    };

    //format_response(res)
}

In fact, the code works well in my home, but when i run it in my company, it cannot request. Use the same computer, the same mobile hotspot.

ecojust avatar Apr 08 '25 07:04 ecojust

I added two permissions to src-tauri/gen/android/app/src/main/AndroidManifest.xml:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Then I modified android:usesCleartextTraffic to true:

android:usesCleartextTraffic="true"

Now it's working properly.

xjuunn avatar Jun 22 '25 03:06 xjuunn