Axios 请求 GET 并在一些 json 修改之后 make POST
Axios request GET and after some json modifying make POST
我正在尝试向 twilio 发出 GET 请求以从特定通道获取数据,之后我需要对其进行一些更改,然后再次 post。
我是 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
函数中执行任何异步操作,因此不需要 return 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));
我正在尝试向 twilio 发出 GET 请求以从特定通道获取数据,之后我需要对其进行一些更改,然后再次 post。
我是 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
函数中执行任何异步操作,因此不需要 return 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));