调用RapidAPI聊天机器人API时提示‘Endpoint不存在’问题求助
对接RapidAPI ChatBot API时遇到"/Endpoint/does not exist/"错误排查
我正在完成大学作业项目,尝试对接RapidAPI上的ChatBot API。直接复制开发者提供的代码后,无论操作如何都会返回‘/Endpoint/does not exist/’错误。我已对代码做了一些修改,但问题仍未解决,以下是我的代码,希望能帮忙排查原因:
function onButtonClick(){ //uses userInput, creates value. const userInput = document.getElementById("userInput").value const value = 0; //basic url for API string to get parsed in to the API call const apiString = "https://chatbot-chatari.p.rapidapi.com/?message=%2F"; console.log(apiString + "API STRING NO MESSAGE") //basic url with message added on, known as value. const apiStringWithMessage = apiString + value; console.log(apiStringWithMessage + "API STRING WITH MESSAGE") //obselete //JSON.stringify(apiStringWithMessage); //setting up options, contains API GET / POST method etc. //Sets up API key and Host URL const options = { method: 'POST', headers: { 'X-RapidAPI-Key': 'myapikey', 'X-RapidAPI-Host': 'chatbot-chatari.p.rapidapi.com' } }; //fetch uses URL with message hardcoded (right now), combined with options. //try catches to find errors, or show result. fetch('https://chatbot-chatari.p.rapidapi.com/?message=%2FHi!', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err)); }
问题排查与修正建议
- 请求方法与参数传递不匹配:你使用
POST方法,但却把消息参数放在URL查询字符串中。绝大多数ChatBot类API的POST请求,要求将消息内容放在请求体(body)内,而非URL参数里,这是导致端点识别错误的核心原因之一。 - 无效的URL构造逻辑:
- 你构造的
apiString包含%2F(斜杠的URL编码),这会干扰API对端点路径的识别; - 定义
value = 0完全不符合消息传递逻辑,而且根本没用到实际获取的userInput值,等于没传递用户输入的消息。
- 你构造的
- 硬编码URL的参数格式错误:最终fetch的URL里
message=%2FHi!这种带编码斜杠的参数,不是API期望的参数格式。
修正后的示例代码
function onButtonClick(){ const userInput = document.getElementById("userInput").value; if (!userInput.trim()) return; // 空输入直接终止,避免无效请求 const options = { method: 'POST', headers: { 'X-RapidAPI-Key': 'myapikey', 'X-RapidAPI-Host': 'chatbot-chatari.p.rapidapi.com', 'Content-Type': 'application/json' // 声明请求体为JSON格式 }, body: JSON.stringify({ message: userInput }) // 将用户输入放在请求体中 }; // 使用干净的端点URL,去掉多余的查询参数 fetch('https://chatbot-chatari.p.rapidapi.com/', options) .then(response => { // 先检查HTTP响应状态,提前捕获非200系列错误 if (!response.ok) throw new Error(`HTTP错误: ${response.status}`); return response.json(); }) .then(response => console.log(response)) .catch(err => console.error(err)); }
额外检查点
- 去RapidAPI的该API文档页面,确认正确的端点路径,有些API的POST端点不是根路径,可能是
/chat这类具体路径; - 检查你的API密钥是否有效,以及是否在RapidAPI后台开通了该API的访问权限;
- 用RapidAPI提供的在线测试工具直接发送请求,确认API本身能正常响应,排除代码外的问题。
内容的提问来源于stack exchange,提问作者Kloakk
相关产品推荐
相关产品推荐

