messagebird/messagebird-nodejs

Conversations API send: Getting "TypeError: Failed to fetch"

angyangie opened this issue · 0 comments

Hi there, I'm getting a "TypeError: Failed to fetch" error when querying any endpoint on the https://conversations.messagebird.com/v1/. I'm most interested in https://conversations.messagebird.com/v1/send. Using Postman, my request gets executed perfectly. Using curl, my request also gets executed perfectly. I copy and paste the same code from Postman or my curl request to my NEXT js React app, and TypeError comes up. I'm both trying the MessageBird SDK and doing the request manually.

This only happens on the messagebird.callflows and messagebird.conversations endpoints. I can hit the messagebird.balance, the messagebird.voice_messages and messagebird.messages endpoints just fine, without any errors, using similar code to that below.

Has anyone else encountered this error on the conversations and callflows API endpoints? Could this be a CORS response error? I see an article here talking about how CORS headers in the MessageBird response could be the culprit: https://bobbyhadz.com/blog/javascript-typeerror-failed-to-fetch-cors.

res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', '*');

Manual request (TypeError):

var myHeaders = new Headers();
myHeaders.append("Authorization", "AccessKey xxxxx");
myHeaders.append("Content-Type", "application/json");

var raw = JSON.stringify({
  "to": "+1908xxxxxxx",
  "from": "xxx channel name xxxx",
  "type": "hsm",
  "content": {
    "hsm": {
      "namespace": "xxxx namespace xxxx",
      "templateName": "pin_generated",
      "language": {
        "policy": "deterministic",
        "code": "es_MX"
      },
      "params": [
        {
          "default": "Sucu Sucu Polanco"
        },
        {
          "default": "9999"
        }
      ]
    }
  }
});

var requestOptions = {
  method: 'POST',
  headers: myHeaders,
  body: raw,
  redirect: 'follow'
};

fetch("https://conversations.messagebird.com/v1/send", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));

SDK request (TypeError):

var messagebird = require('messagebird')(JSON.stringify( xxxxx token xxxxx));

        var params = {
            "to": "+1908xxxxxx",
            "from": "xxx channel name xxxx",
            "type": "hsm",
            "content": {
                "hsm": {
                "namespace": "xxxx namespace xxxx"",
                "templateName": "pin_generated",
                "language": {
                    "policy": "deterministic",
                    "code": "es_MX"
                },
                "params": [
                    {
                    "default": "Sucu Sucu Polanco"
                    },
                    {
                    "default": "9999"
                    }
                ]
                }
            }
        }

        messagebird.conversations.start(params, function(err, response) {
        if (err) {
            console.log(err.error)
        }
        console.log(response);
        });

Curl request (NO error):

curl -X "POST" "https://conversations.messagebird.com/v1/send" \
-H "Authorization: AccessKey xxxxxxxx" \
-H "Content-Type: application/json" \
--data '{
    "type": "hsm",
    "to": "+1908xxxxxx",
    "from": "xxxx channelID xxxxx",
    "content": {
        "hsm": {
            "namespace": "xxxx namespace xxxxx",
            "templateName": "pin_generated",
            "language": {
                "policy": "deterministic",
                "code": "es_MX"
            },
            "params": [
                {
                    "default": "Sucu Sucu Polanco"
                },
                {
                    "default": "7896"
                }
            ]
        }
    }
}'

PostMan (NO error):

Screenshot 1:

Screen Shot 2022-11-29 at 5 44 50 PM

Screenshot 2:

Screen Shot 2022-11-29 at 5 45 40 PM