You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rust reqwest请求SquareCloud API返回403 Forbidden问题求助

reqwest请求SquareCloud API返回403 Forbidden的解决方案

问题描述

使用Rust的reqwest库向SquareCloud API发送GET请求时,始终返回403 Forbidden,响应内容为Cloudflare的拦截页面。但使用Python、Insomnia以及Rust的ureq库测试时,请求均能正常完成。

原因分析

Cloudflare的安全机制会基于请求特征(比如User-Agent头)识别请求合法性。reqwest默认的User-Agent为reqwest/{version},这类标识可能被判定为非标准客户端请求从而触发拦截;而ureq等工具的默认User-Agent更接近常规客户端,因此可以通过检测。

解决方案

通过修改reqwest请求的User-Agent头,模拟能正常通过检测的客户端标识,即可绕过Cloudflare的拦截。

修改后的代码示例

use reqwest::{self, header};

#[tokio::main]
async fn main() {
    let mut headers = header::HeaderMap::new();
    // 添加授权头
    headers.insert("authorization", "<authorization>".parse().unwrap());
    // 设置与ureq一致的User-Agent,或使用常见浏览器标识
    headers.insert(
        header::USER_AGENT,
        "ureq/2.9.0".parse().unwrap()
    );

    // 构建带有默认头的客户端
    let client = reqwest::Client::builder()
        .default_headers(headers)
        .build()
        .unwrap();

    let response = client.get("https://api.squarecloud.app/v2/user")
        .send()
        .await
        .unwrap();

    println!("{:?}", response);
}

额外验证

可以使用无需token的测试接口https://api.squarecloud.app/v2/service/statistics验证修改后的代码是否正常工作,确认User-Agent的调整是否解决了拦截问题。

内容的提问来源于stack exchange,提问作者meirochou

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 11:28:11