0

I am trying to concat two encodedUri string and I am unable to do it. I am able to send message, but I am receiving only the text message, not the links. Can anyone suggest me any way to solve this problem? Here is my tried solution:

const sms = async (phoneNumber, textMessage, senderId) => {
  //   if (!text || typeof text != "string") {
  //     throw new TypeError("Second argument text is required, it must be string");
  //   }
  try {
    const link = "https://google.com/uLK1aeSA98DDrktU7/";
    const links = encodeURI(link);

    const plainText = encodeURI(textMessage);
    const text = plainText.concat(links);

    const response = await request({
      method: "POST",
      uri: `https://www.smsalert.co.in/api/push.json?apikey=${apiKey}&sender=${senderId}&mobileno=${phoneNumber}&text=${text}`,
      json: true,
    });
    // console.log(response);
    return response;
  } catch (error) {
    console.error(error);
  }
};
sms("+91xxxxxx", "Have you Checked-in.", "FUNPRA");

output i am getting

Have%20you%20Checked-in.https://google.com/uLK1aeSA98DDrktU7/
thelr
  • 1,134
  • 11
  • 30
  • You want `encodeURIComponent`, not `encodeURI`. You can also just use `+` instead of `.concat(...)`, but it doesn't really matter. – Heretic Monkey May 26 '20 at 13:17
  • @sidgate The sample `textMessage` is given in the last line's `sms("+91xxxxxx", "Have you Checked-in.", "FUNPRA");` call. – Heretic Monkey May 26 '20 at 13:20
  • 1
    Does this answer your question? [Should I use encodeURI or encodeURIComponent for encoding URLs?](https://stackoverflow.com/questions/4540753/should-i-use-encodeuri-or-encodeuricomponent-for-encoding-urls) – Heretic Monkey May 26 '20 at 13:21
  • Please post "Output I am expecting" too – mplungjan May 26 '20 at 13:42

1 Answers1

1

Did you mean

const text = "Have you Checked-in.";
const url = "https://google.com/uLK1aeSA98DDrktU7/"
let link = new URL(url+text);
console.log(link.href)

// or 

const uri = encodeURIComponent(url+text)
console.log(uri)
mplungjan
  • 169,008
  • 28
  • 173
  • 236