twilio-node
twilio-node copied to clipboard
TypeError [ERR_INVALID_ARG_TYPE] webhook when the request type is application/json
Issue Summary
I was using Twilio Studio setting up an appointment reminder, for the confirmation widget, I setup the request and content as below:
and my server code is simply like below:
app.post('/webhook/twilio/sms/confirm_appointment', twilio.webhook(authToken, {}), (req, res) => {
// Twilio Messaging URL - receives incoming messages from Twilio
console.log('sms received', req.body);
res.send(200);
});
When the confirmation callback hit my server, Twilio webhook throws:
TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be one of type string, Buffer, ArrayBuffer, Array, or Array-like Object. Received type object
app_1 | at Function.from (buffer.js:232:9)
app_1 | at getExpectedBodyHash (/app/node_modules/twilio/lib/webhooks/webhooks.js:84:20)
app_1 | at validateBody (/app/node_modules/twilio/lib/webhooks/webhooks.js:116:22)
app_1 | at validateRequestWithBody (/app/node_modules/twilio/lib/webhooks/webhooks.js:133:5)
app_1 | at validateExpressRequest (/app/node_modules/twilio/lib/webhooks/webhooks.js:171:12)
app_1 | at hook (/app/node_modules/twilio/lib/webhooks/webhooks.js:253:21)
app_1 | at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
app_1 | at next (/app/node_modules/express/lib/router/route.js:137:13)
app_1 | at Route.dispatch (/app/node_modules/express/lib/router/route.js:112:3)
app_1 | at Layer.handle [as handle_request] (/app/node_modules/express/lib/router/layer.js:95:5)
app_1 | Mon, 12 Oct 2020 06:41:23 GMT express deprecated res.send(status, body): Use res.status(status).send(body) instead at src/middlewares/errorHandling.ts:16:13
I checked the twilio source code, mainly caused by this function:
function getExpectedBodyHash(body) {
return crypto
.createHash('sha256')
.update(Buffer.from(body, 'utf-8'))
.digest('hex');
}
obviously if the request type is application/json, express will parse the request.body to an object right away, which causes the issue.
By changing the request type to "Form URL Encoded" fixes my issue:
Steps to Reproduce
- Set up Twilio webhook middleware in your express server
- Set up a confirmation callback request to your server with request type to application/json
Technical details:
- twilio-node version: [email protected]
- node version: 10.x
Hello @neekey,
Thank you for taking the time to report this!
I was able to reproduce this error locally. This issue has been added to our internal backlog to be prioritized. Pull requests and +1s on the issue summary will help it move up the backlog.
With best regards,
Elmer
+1 I'm having this issue in production on Heroku. Was working fine, and then just cropped up after I sanitized phone numbers to be formatted like this: +13334445555, which I thought was the ideal format. It works from localhost. I double checked my config vars and they are correct in production. Not sure why it would be different in production than in local.
twilio.messages
.create({
body: `Here\'s your code: ${user.twoFactorAuthToken}. It expires in 10 minutes.`,
from: process.env.TWILIO_SENDER_NUMBER,
to: user.phone
})
.then(message => console.log("Twilio sent: ", message.sid))
.catch(err => console.log("Twilio error: ", err));
2021-12-23T19:06:27.622584+00:00 app[web.1]: Creating a signature for the request...
2021-12-23T19:06:27.626309+00:00 app[web.1]: Twilio error: TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received undefined
2021-12-23T19:06:27.626310+00:00 app[web.1]: at new NodeError (node:internal/errors:371:5)
2021-12-23T19:06:27.626310+00:00 app[web.1]: at prepareSecretKey (node:internal/crypto/keys:570:11)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at new Hmac (node:internal/crypto/hash:132:9)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at Object.createHmac (node:crypto:162:10)
2021-12-23T19:06:27.626311+00:00 app[web.1]: at createSignature (/app/src/sumsub.js:22:28)
2021-12-23T19:06:27.626312+00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:96:5) {
2021-12-23T19:06:27.626313+00:00 app[web.1]: code: 'ERR_INVALID_ARG_TYPE'
2021-12-23T19:06:27.626314+00:00 app[web.1]: }
I updated to "twilio": "^3.72.0" and still not working.
Anyone have an answer to this? https://stackoverflow.com/questions/70560702/twilio-error-typeerror-err-invalid-arg-type-the-key-argument-must-be-of-ty
@thinkingserious This problem is still blocking my production sending SMS's. Any progress?
Still busted
I'm having issue in production with it. Any progress?
Fixed by https://github.com/twilio/twilio-node/pull/665