How to add variables concatenated into a url link?

Asked

Viewed 84 times

0

I have the following code:


var config = {
    method: "post",
    url:
      "https://my-api-link/v2/[email protected]&token=EAD7FC0724EF4254FG34323D62FEF2",
    headers: {}
  };

I wanted to save the email and the token which are global variables,:

const email = proccess.env.EMAIL
const token = proccess.env.TOKEN

var config = {
    method: "post",
    url:
      "https://my-api-link/v2/sessions?email=$email&token=$token",
    headers: {}
  };

I tried that other way:

const email = proccess.env.EMAIL
const token = proccess.env.TOKEN

var config = {
    method: "post",
    url:
      "https://my-api-link/v2/sessions?email="+email"&token="+token"",
    headers: {}
  };

But also unsuccessfully,!

Thanks in advance!

1 answer

2


You could try the following syntax:

const email = proccess.env.EMAIL
const token = proccess.env.TOKEN

var config = {
    method: "post",
    url:
      `https://my-api-link/v2/sessions?email=${email}&token=${token}`,
    headers: {}
  };

Note that in my solution I used the template interpolation of strings by string template. To use the template string just start the string with acento grave (`).

Then to interpolate the string just use one ${valor} with the value to be concatenated.

More information here

  • THANK YOU! DONE!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.