Twilio请求URL端点返回JSON却提示Content-Type错误排查
解决Twilio Voice API的Content-Type错误问题
问题核心:Twilio的Voice Request URL要求接收TwiML格式的XML响应,但你把返回JSON的
/make-call端点配置成了该URL,导致Twilio无法解析响应内容,触发Content-Type错误。正确配置逻辑:
- 将TwiML应用的Voice Request URL改为你的
/twiml端点(假设该端点负责生成TwiML) make-call的作用是触发呼叫发起逻辑,不需要设置为Voice Request URL,它只需调用Twilio API发起呼叫,并在参数中指定返回TwiML的端点地址
- 将TwiML应用的Voice Request URL改为你的
代码修正示例(以Express框架为例):
返回TwiML的端点必须设置正确的响应头:app.get('/twiml', (req, res) => { const twiml = new twilio.twiml.VoiceResponse(); twiml.say('这是来自Twilio的呼叫'); // 关键:设置响应Content-Type为XML res.set('Content-Type', 'application/xml'); res.send(twiml.toString()); });发起呼叫的
/make-call端点只需处理呼叫触发:app.post('/make-call', async (req, res) => { const client = twilio(accountSid, authToken); try { const call = await client.calls.create({ url: 'https://你的服务器地址/twiml', // 指向返回TwiML的端点 to: req.body.to, from: '你的Twilio号码' }); res.json({ success: true, callSid: call.sid }); } catch (err) { res.status(500).json({ error: err.message }); } });额外检查项:
- 确认TwiML应用的Voice Request URL配置的是
/twiml这类返回XML的端点,而非返回JSON的/make-call - 确保TwiML端点的响应头
Content-Type严格为application/xml,不能是JSON或其他类型
- 确认TwiML应用的Voice Request URL配置的是
内容的提问来源于stack exchange,提问作者VikR
相关产品推荐
相关产品推荐

