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

使用VirusTotal API扫描URL时遭遇400 Bad request错误求助

解决VirusTotal API扫描URL返回400 Bad Request的问题

你的代码核心问题是VirusTotal v3 API的URL扫描端点不支持JSON格式的请求体,它要求使用表单编码(application/x-www-form-urlencoded)的方式提交数据。

修改后的代码

import requests

def scan_url(api_key, url):
    vt_api_url = "https://www.virustotal.com/api/v3/urls"
    headers = {
        "x-apikey": api_key,
        # 移除JSON格式的Content-Type声明,使用data参数时requests会自动添加正确的表单编码头
    }
    data = {
        "url": url,
    }
    # 将json=data改为data=data,发送表单编码格式的请求体
    response = requests.post(vt_api_url, headers=headers, data=data)
    if response.status_code == 201:
        return response.json()
    else:
        print(f"Failed to scan URL. Status code: {response.status_code}")
        # 打印API返回的错误详情,精准定位问题
        print("API error details:", response.text)
        if response.status_code == 400:
            print("Bad request. Check request format or URL validity.")
        elif response.status_code == 403:
            print("Forbidden. Check if your API key is valid and has proper permissions.")
        elif response.status_code == 429:
            print("Rate limit exceeded. Too many requests. Try again later.")
        else:
            print("Unknown error occurred.")
        return None

api_key = "***"
url = "https://www.blogger.com"
result = scan_url(api_key, url)
if result:
    print(result)

额外排查建议

  • 打印response.text查看API返回的具体错误描述,VirusTotal会明确说明错误原因(比如请求格式不符、URL包含非法字符等)
  • 确认API密钥是从VirusTotal控制台获取的有效密钥,且已开通URL扫描权限
  • 确保目标URL没有空格、未转义的特殊字符(如&、#等,若有需进行URL编码)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 14:06:03