Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
angyangie opened this issue Nov 29, 2022 · 0 comments
Open

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

angyangie opened this issue Nov 29, 2022 · 0 comments

Comments

@angyangie
Copy link

angyangie commented Nov 29, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant