Axios请求GET,并在一些json修改后使POST



我正试图向twilio发出GET请求,以便从特定通道获取数据,之后我需要对其进行一些更改,然后再次发布。

我是全新的工作与js我会感谢任何建议我没有使用Twilio SDK

到目前为止,我做了这个…但是它没有发出post请求

function modifyChannel(sid, json) {
console.log(json);
return new Promise((resolve, reject) => {
let newJson = JSON.parse(json.attributes);
newJson.task_sid = null;
json.attributes = JSON.stringify(newJson);
resolve(json);
})
}
function postChannel(sid, json) {
axios({
method: 'post',
url:`https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${sid}`,

auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token
},
data: {
json
}
});
}
axios({
method: 'get',
url:`https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${channel_sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token
}
})
.then(response => {
return modifyChannel(channel_sid, response.data);
}).then(jsonModified => { postChannel(channel_sid, jsonModified); })
.catch(err => console.log(err));

这里是Twilio开发者布道者。

我认为问题是当你发出post请求时,你正在传递data: { json }。它会扩展到data: { json: { THE_ACTUAL_DATA }}而你只需要data: { THE_ACTUAL_DATA }。因此,从那里删除json键。

您还可以通过数据操作来简化事情。你在modifyChannel函数中没有做任何异步的事情,所以没有必要返回一个Promise。

试试下面的命令:

function modifyChannel(sid, json) {
let newJson = JSON.parse(json.attributes);
newJson.task_sid = null;
json.attributes = JSON.stringify(newJson);
return json;
}
function postChannel(sid, json) {
axios({
method: "post",
url: `https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token,
},
data: json,
});
}
axios({
method: "get",
url: `https://chat.twilio.com/v2/Services/${DEV_CREDENTIAL.programmableChatSid}/Channels/${channel_sid}`,
auth: {
username: DEV_CREDENTIAL.account,
password: DEV_CREDENTIAL.token,
},
})
.then((response) => {
const jsonModified = modifyChannel(channel_sid, response.data);
postChannel(channel_sid, jsonModified);
})
.catch((err) => console.log(err));