twilio-labs / plugin-webhook

Twilio CLI plugin to emulate webhook requests from Twilio
MIT License
10 stars 2 forks source link

connect ECONNREFUSED ::1:7071 when testing Azure Function locally #2

Open Swimburger opened 2 years ago

Swimburger commented 2 years ago

I'm using Azure Functions on macOS to build a Twilio app and I run it using func start (Azure Functions CLI), with the following output

...
Functions:

    IncomingMessage: [POST] http://localhost:7071/api/IncomingMessage

    MessageStatus: [POST] http://localhost:7071/api/MessageStatus

When I try to emulate a webhook request like this:

twilio webhook:invoke http://localhost:7071/api/IncomingMessage --no-signature

I get the following error:

» connect ECONNREFUSED ::1:7071
 » twilio-cli encountered an unexpected error. To report this issue, execute the command with the "-l debug" flag, then copy the output to a new issue here: "https://github.com/twilio-labs/plugin-webhook/issues"

Here's the output with debug logs:

[DEBUG] Config File: /Users/nswimberghe/.twilio-cli/config.json
[DEBUG] Using profile: Corp
[DEBUG] Signature: ""
[DEBUG] {"message":"connect ECONNREFUSED ::1:7071","name":"Error","stack":"Error: connect ECONNREFUSED ::1:7071\n    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1247:16)","config":{"transitional":{"silentJSONParsing":true,"forcedJSONParsing":true,"clarifyTimeoutError":false},"transformRequest":[null],"transformResponse":[null],"timeout":0,"xsrfCookieName":"XSRF-TOKEN","xsrfHeaderName":"X-XSRF-TOKEN","maxContentLength":-1,"maxBodyLength":-1,"headers":{"Accept":"*/*","Content-Type":"application/x-www-form-urlencoded","Accept-Encoding":"gzip","I-Twilio-Idempotency-Token":"aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee","User-Agent":"TwilioProxy/1.1","X-Home-Region":"us1","X-Twilio-Signature":"","Content-Length":560},"method":"post","url":"http://localhost:7071/api/IncomingMessage","data":"AccountSid=[REDACTED]&AddOns=%7B+%22status%22%3A+%22successful%22%2C+%22message%22%3A+null%2C+%22code%22%3A+null%2C+%22results%22%3A+%7B+%7D+%7D&ApiVersion=2010-04-01&From=%2B15017122661&FromCity=SAN+FRANCISCO&FromCountry=US&FromState=CA&FromZip=94903&To=%2B15558675310&ToCity=SAN+FRANCISCO&ToCountry=US&ToState=CA&ToZip=94105&Body=Ahoy&MessageSid=SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&NumMedia=0&NumSegments=1&ReferralNumMedia=0&SmsMessageSid=SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&SmsSid=SMaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&SmsStatus=received"},"code":"ECONNREFUSED","status":null}
 » connect ECONNREFUSED ::1:7071
[DEBUG] Found command "webhook:invoke" plugin: @twilio-labs/plugin-webhook
 » twilio-cli encountered an unexpected error. To report this issue, execute the command with the "-l debug" flag, then copy the output to a new issue here: "https://github.com/twilio-labs/plugin-webhook/issues"
[DEBUG] REQUEST_ERROR
[DEBUG] Error: REQUEST_ERROR
    at InvokeCommand.run (/Users/nswimberghe/.twilio-cli/node_modules/@twilio-labs/plugin-webhook/src/commands/webhook/invoke.js:48:13)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async InvokeCommand._run (/Users/nswimberghe/.twilio-cli/node_modules/@oclif/core/lib/command.js:81:22)
    at async Config.runCommand (/usr/local/lib/node_modules/twilio-cli/node_modules/@oclif/config/lib/config.js:173:24)
    at async Main.run (/usr/local/lib/node_modules/twilio-cli/node_modules/@oclif/command/lib/main.js:27:9)
    at async Main._run (/usr/local/lib/node_modules/twilio-cli/node_modules/@oclif/command/lib/command.js:43:20)

Manually sending an HTTP request using PowerShell works fine:

Invoke-WebRequest http://localhost:7071/api/IncomingMessage `
    -Method POST `
    -Body @{Body="Hello world!"} `
    -ContentType "application/x-www-form-urlencoded"

response is:

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Message action="/api/MessageStatus" method="POST">You sent: Hello world!</Message>
</Response>
Swimburger commented 2 years ago

FYI, using the equivalent localhost IP address 127.0.0.1 does work:

twilio webhook:invoke http://127.0.0.1:7071/api/IncomingMessage --no-signature
Swimburger commented 2 years ago

We discovered that this is not an issue when using Node.js LTS (v16.17.0), but it is an issue when using latest (v18.7.0).