0

I am trying to send a media message via Twilio conversations api. In the documentation it shows and example using cURL like this below

curl -u "<account_sid>:<account_secret>" -X POST https://conversations.twilio.com/v1/Conversations/<conversation_sid>/Messages -d MediaSid=<media_sid>

Here is a link to the docs, its at the bottom of the page when scrolling down look for "Using Media Messaging via the Conversations REST API"

https://www.twilio.com/docs/conversations/media-support-conversations#using-media-messaging-via-the-conversations-rest-api

I am using Node.js for backend so I need to convert this cURL to an axios post request. Note: in the above example the url in the docs shows https://conversations.twilio.com/v1/Conversations/<conversation_sid>/Messages but I need to do the url below because I am using subaccounts. The solution below is giving me the error "Message body not provided"

data: {
      code: 50503,
      message: 'Message body not provided',
      more_info: 'https://www.twilio.com/docs/errors/50503',
      status: 400
    }
await  axios({ // post request to create media message via conversations REST api
                    url:`https://conversations.twilio.com/v1/Services/${convoServiceSid}/Conversations/${stringSenderId}/Messages`,
                    method:"POST",
                    headers: {
                        'Content-Type': 'x-www-form-urlencoded'
                      },
                    auth: { username: subActServiceSid, password: subActAuthToken },
                    body: new URLSearchParams({
                        MediaSid: 'ME58052d0b6e178ace30a0dbb617ffec2c'
                      })
                    }).then(res => {
                        console.log("res from first post request to twilio with fileData", res.data)
                        })
KingJoeffrey
  • 303
  • 5
  • 16
  • I believe, in Axios post data is sent using `data` property and not `body` – Vin_it Jul 03 '22 at 06:15
  • I tried that no luck.... – KingJoeffrey Jul 03 '22 at 06:33
  • Another issue is, `data: new URLSearchParams({ MediaSid: 'ME58052d0b6e178ace30a0dbb617ffec2c' })` which doesn't make sense in this context, I think you want to `data: JSON.stringify({ MediaSid: 'ME58052d0b6e178ace30a0dbb617ffec2c' })` OR `data: { MediaSid: 'ME58052d0b6e178ace30a0dbb617ffec2c' }` – Vin_it Jul 03 '22 at 06:35
  • Did you try mediaSid instead? I had some issues because twilio conversations api is case sensitive. – ealvess Aug 20 '22 at 14:38
  • Another thing, the MediaSid field is need to be outside of body, body field is just to send the message up to 1600 characters. cheers! – ealvess Aug 20 '22 at 14:48

0 Answers0