首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用WhatsApp axios.post连接TypeScript API

使用WhatsApp axios.post连接TypeScript API
EN

Stack Overflow用户
提问于 2022-08-30 12:49:23
回答 1查看 33关注 0票数 0

我刚刚开始使用WhatsApp Cloud。我以提供的关于故障的示例作为参考,但是由于我采用的是无服务器的方法,所以有些事情是不同的。

从glitch的例子中可以看出,它使用了axios(config)方法,并且我尝试了它,在做了一些微小的更改之后,它工作得很好,但是当我尝试axios.post()方法时,我继续得到以下错误:

AxiosError:状态代码为400的请求失败

axios(config)方法(起作用)

代码语言:javascript
运行
复制
await axios({
                    method: "POST", // Required, HTTP method, a string, e.g. POST, GET
                    url:"https://graph.facebook.com/{{Version}}/{{Phone-Number-ID}}/messages?access_token={{Token}}",
                    data: {
                        messaging_product: "whatsapp",
                        recipient_type: "individual",
                        to: {{Recipient-Phone-Number}},
                        text: {body: "Welcome back"},
                    },
                    headers: {"Content-Type": "application/json"},
                }); 

axios.post()方法(不起作用)

代码语言:javascript
运行
复制
let url = "https://graph.facebook.com/{{Version}}/{{Phone-Number-ID}}/messages" 
let payload = {
    messaging_product: "whatsapp",
    recipient_type: "individual",
    to: {{Recipient-Phone-Number}},
    text: {body: "Welcome back my friend"},
}

let  headers = {"Content-Type": "application/json", "Authorization":"Bearer {{token}}"
}

let params = {}
try
{
    const resp = await axios.post(url, {payload}, {headers, params});

    log("POST RESP",resp)
}
catch(error)
{
    throw error;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-30 13:25:06

试试下面的代码。

代码语言:javascript
运行
复制
let url = 'https://graph.facebook.com/<Version>/<Your Phone number ID>/messages';

let payload = {
            'messaging_product': 'whatsapp',
            'recipient_type': 'individual',
            'to': '123456789012',//Recipient Phone Number
            'type': 'text',
            'text': {
                'body': 'Welcome back my friend'
            }
        };

let  headers = {
                'Authorization': 'Bearer <Your Temporary access token>',
                'Content-Type': 'application/json'
               };

  axios.post(url, payload, {
    headers: headers
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73542755

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档