Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

130
Vistas
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.

I am completely new working with js I will appreciate any advice I am not using 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));
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Twilio developer evangelist here.

I think the issue is that you are passing data: { json } when you make the post request. That's going to expand to: data: { json: { THE_ACTUAL_DATA }} where you just want data: { THE_ACTUAL_DATA }. So, remove the json key from there.

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.

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));
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda