[英]Axios request GET and after some json modifying make POST
I am trying to make a GET request to twilio to get the data from a specific channel after that I need make some changes to it and then post it again.我正在尝试向 twilio 发出 GET 请求以从特定通道获取数据,之后我需要对其进行一些更改,然后再次发布。
I am completely new working with js I will appreciate any advice I am not using Twilio SDK我是使用 js 的全新工作,我将不胜感激我不使用 Twilio SDK 的任何建议
So far I make this.. but It doesn't make the post request到目前为止,我做了这个......但它没有提出发布请求
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 developer evangelist here. Twilio 开发人员布道者在这里。
I think the issue is that you are passing data: { json }
when you make the post request.我认为问题在于您在发出发布请求时正在传递data: { json }
。 That's going to expand to: data: { json: { THE_ACTUAL_DATA }}
where you just want data: { THE_ACTUAL_DATA }
.这将扩展为: data: { json: { THE_ACTUAL_DATA }}
您只需要data: { THE_ACTUAL_DATA }
。 So, remove the json
key from there.因此,从那里删除json
密钥。
You can also simplify things with your data manipulation.您还可以通过数据操作来简化事情。 You aren't doing anything asynchronous in your modifyChannel
function, so there's no need to return a Promise.您没有在modifyChannel
函数中执行任何异步modifyChannel
,因此无需返回 Promise。
Try the following instead:请尝试以下操作:
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));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.