2

This must be a stupid question, but I'm just starting and would appreciate any help!

So I have this code to get query parameter:

app.get('/', (req, res) => {
var code = req.query.code;
console.log(code);

And when I go to http://localhost:3000/?code=123, I get the code value in console, so it works fine.

Now, I need to send a GET request and add the value of the var code, this is where I'm stuck. Let's say, I should send a GET request to 'http://testtesttest123.com/' + var code + 'hi'.

How can I do this?

I've tried this way and some other ways, but nothing worked:

 axios.get('http://testtesttest123.com/?&code=', {params: {code}}, '&hi')
.then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  })
});

Thank you in advance!

3
  • What exactly is the final URL meant to look like? Is it http://testtesttest123.com/?code=123&hi or something else? Commented Jun 1, 2021 at 23:10
  • yes, exactly - testtesttest123.com/?code=123&hi Commented Jun 1, 2021 at 23:27
  • Multiple questions and answers related to this topic are already available. Checkout this stackoverflow.com/questions/58522972/… Commented Jun 2, 2021 at 5:54

2 Answers 2

3

The axios.get call should look like this.

axios.get('http://testtesttest123.com/?code=' + code + '&hi')

With code = 123, this will call the URL http://testtesttest123.com/?code=123&hi.

Sign up to request clarification or add additional context in comments.

Comments

1

Use the params config to send through query parameters. To support your empty hi parameter, you can include it in the URL string

axios.get("http://testtesttest123.com/?hi", {
  params: { code }
})

For a code value of 123, this will perform a GET request to

http://testtesttest123.com/?hi&code=123

It will also ensure that the code value is made safe for use in URLs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.