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

189
Views
how pass a var in the axios?

I'm trying to pass a var to the axios more by what I'm seeing it just accepts string is there any way to pass a var ?

My code:

router.get('/albuns', async (req, res)=>{
  const response = await axios.get(req.query.id)

  return console.log(response)
})

Error:

Argument of type 'string | ParsedQs | string[] | ParsedQs[] | undefined' is not assignable to parameter of type 'string'.

Type 'undefined' is not assignable to type 'string'.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

This is just a Typescript warning because axios.get() expects a string argument but the default req.query can provide various types.

You just need to cast the value

const response = await axios.get(req.query.id as string);

Another option is to type the request in your handler signature

import { Request } from "express";

interface AlbunsQuery {
  id: string;
}

type AlbunsRequest = Request<{}, any, any, AlbunsQuery>;

router.get("/albuns", async (req: AlbunsRequest, res) => {
  const response = await axios.get(req.query.id);

  // etc
});
about 4 years ago · Juan Pablo Isaza Report

0

URL parameters aren't supported, and it seems they have no intention on adding it, given they expect you to use template strings. Here there's a discussion: https://github.com/axios/axios/issues/706

Axios GET method takes two arguments: the first being the URL string, and the second argument, we have it configure object with the property called params.

Axios Code:

axios.get("http://localhost/example"){
       params{
          name: myName
          example: 2
}}

Express Code:

app.get("example", (req, res){
const name : request.query.name
const example: request.query.example
}
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!