如何修复使用Axios调用API时出现的400状态码请求错误
修复400请求错误的方案
核心问题:参数传递方式错误
aztro API要求POST请求的参数(sign和day)必须放在请求体中,而非URL查询参数里。你的代码把参数放在了params字段(对应URL拼接的查询参数),这是触发400错误的直接原因。
修复后的代码
const getZodicInfo = (sign, day) => { const options = { method: "POST", url: "https://sameer-kumar-aztro-v1.p.rapidapi.com/", // 将params替换为data,把参数放入请求体 data: { sign: sign, day: day }, headers: { "X-RapidAPI-Key": "edc6bd0575msh7fb7aa8058de303p15ffd6jsn551bf87a46dd", "X-RapidAPI-Host": "sameer-kumar-aztro-v1.p.rapidapi.com", }, }; return axios .request(options) .then(function (response) { return response.data; }) .catch(function (error) { console.error(error); }); };
额外验证项
- 检查
sign参数:必须是API支持的全小写星座名称,比如aries、taurus等,拼写错误会触发400。 - 检查
day参数:只能是today、tomorrow或yesterday三者之一,其他值会返回错误。 - 确认RapidAPI Key状态:如果Key过期、额度耗尽或权限不足,也可能导致400类错误,可在RapidAPI控制台查看Key有效性。
内容的提问来源于stack exchange,提问作者sakshi
相关产品推荐
相关产品推荐

