繁体   English   中英

Axios 请求 GET 和一些 json 修改后 make POST

[英]Axios request GET and after some json modifying make POST

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

我是使用 js 的全新工作,我将不胜感激我不使用 Twilio SDK 的任何建议

到目前为止,我做了这个......但它没有提出发布请求

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 开发人员布道者在这里。

我认为问题在于您在发出发布请求时正在传递data: { json } 这将扩展为: data: { json: { THE_ACTUAL_DATA }}您只需要data: { THE_ACTUAL_DATA } 因此,从那里删除json密钥。

您还可以通过数据操作来简化事情。 您没有在modifyChannel函数中执行任何异步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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM