Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!