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

Node.js调用Roboflow API出现500状态码问题求助

解决Roboflow API在Node.js中返回500状态码的问题
  • 修复异步文件写入的时序问题
    你当前用await配合回调版的fs.writeFile,但await无法等待回调执行完成,会导致后续fs.readFileSync读取到未完全写入的文件,这很可能是触发API 500错误的核心原因。替换为Promise版文件写入:

    const imagePath = join(__dirname, 'image.jpg');
    // 改用promise版writeFile,确保文件写入完成后再执行后续操作
    await fs.promises.writeFile(imagePath, imageFile.data);
    
  • 修正请求的Content-Type
    Roboflow推理API不支持application/x-www-form-urlencoded作为图片数据的请求头。根据你的数据类型选择正确的Content-Type:

    • 直接发送二进制图片数据(推荐,更高效):
      const busResponse = await axios({
        method: "POST",
        url: `${INFERENCE_API_URL}/vehicle-detection-bz0yu/4`,
        params: { api_key: INFERENCE_API_KEY },
        data: imageFile.data, // 直接使用原始二进制Buffer,无需存文件再读取
        headers: { "Content-Type": "image/jpeg" },
        timeout: 50000
      });
      
    • 若必须发送base64编码的图片:
      // 直接从二进制数据转base64,跳过文件读写步骤
      const image = imageFile.data.toString('base64');
      const busResponse = await axios({
        method: "POST",
        url: `${INFERENCE_API_URL}/vehicle-detection-bz0yu/4`,
        params: { api_key: INFERENCE_API_KEY },
        data: image,
        headers: { "Content-Type": "image/jpeg" },
        timeout: 50000
      });
      
  • 简化流程(可选)
    完全可以跳过本地文件存储步骤,直接使用imageFile.data发起请求,减少IO操作的同时避免文件相关的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 11:58:14