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

React Native用fetch请求getmati API Postman正常但返回400错误如何解决

问题根因
  • React Native 内置的 fetch API 对 URLSearchParams 类型的请求体处理存在兼容性缺陷,你传入的 grant_type 参数没有被正确序列化为 x-www-form-urlencoded 格式,导致服务端无法识别到该参数,返回400错误。
修复方案

你可以选择以下任意一种方式修改代码即可解决问题:

  1. 直接传入字符串格式的表单参数(兼容性最好)
const myoptions = {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded',
        Authorization:
          'Basic 替换为你自己的client_id:client_secret对应的Base64编码值',
      },
      body: 'grant_type=client_credentials',
    };

    fetch('https://api.getmati.com/oauth', myoptions)
      .then(response => response.json())
      .then(response => console.log(response))
      .catch(err => console.error(err));
  1. 手动对URLSearchParams执行序列化
    如果你要保留URLSearchParams的写法,手动调用toString()方法即可:
body: new URLSearchParams({grant_type: 'client_credentials'}).toString()
额外验证提示

请同时确认你的Authorization请求头中的凭证有效性:Basic 后拼接的内容必须是你的client_id:client_secret组合字符串经过Base64编码后的结果,确保和Postman中配置的一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 09:15:04