我正在使用Vonage进行一个信使项目。这段代码运行良好,但我尝试将其转换为axios而不是require.js。
var request = require("request");
const data = JSON.stringify({
from: { type: "messenger", id: process.env.BOT_ID },
to: { type: "messenger", id: process.env.USER_ID },
message: {
content: {
type: "text",
text: "Hi There!",
},
},
});
var options = {
url: "https://messages-sandbox.nexmo.com/v0.1/messages",
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: data,
auth: {
user: process.env.API_KEY,
pass: process.env.API_SECRET_KEY,
},
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);这是带有相同请求的axios版本:
axios({
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
body: {
from: { type: "messenger", id: process.env.BOT_ID },
to: { type: "messenger", id: process.env.USER_ID },
message: {
content: {
type: "text",
text:
"Hi There!"
},
},
},
auth: {
user: process.env.API_KEY,
pass: process.env.API_SECRET_KEY,
},
url: "https://messages-sandbox.nexmo.com/v0.1/messages",
});但不起作用。我也试过
车身
发布于 2021-03-02 20:52:13
Vonage支持给我发送了这段代码,它起了作用(没有人也没有数据)
axios
.post(
"https://messages-sandbox.nexmo.com/v0.1/messages",
{
from: { type: "messenger", id: "107083064136738" },
to: { type: "messenger", id: "3958866477502819" },
message: {
content: {
type: "text",
text: "You should havee ",
},
},
},
{
auth: {
username: "5aa06d41",
password: "FMqffmop1GB64X58",
},
}
)
.then(function (response) {
console.log("Status: " + response.status);
console.log(response.data);
})
.catch(function (error) {
console.error(error);
});发布于 2021-03-02 08:59:24
设置主体数据的axios属性是" data ",而不是" body“。这件事以前就把我搞晕了。请尝试使用一些控制台日志来检查是否发送了正确的请求:
axios({
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
data: {
from: { type: "messenger", id: process.env.BOT_ID },
to: { type: "messenger", id: process.env.USER_ID },
message: {
content: {
type: "text",
text:
"Hi There!"
},
},
},
auth: {
user: process.env.API_KEY,
pass: process.env.API_SECRET_KEY,
},
url: "https://messages-sandbox.nexmo.com/v0.1/messages",
});https://stackoverflow.com/questions/66433653
复制相似问题