0

I am trying to get data by passing multiple ids as param.But the params i am passing is going as array which i don't want.

Example:

Current data: The api is returning param as details?ids[]=123&ids[]=245

Expected data: I would like to pass the param as details?ids=123&ids=245

  const postRes = await axios.post('employee/departments', this.details);
  const getRes = await axios.get('employee/departments',
     { params: { ids: postRes.map(i=>i.id)},
  });
  this.$emit('fileDetails', getRes.data);
1
  • The method is working fine and returning GET api back.The issue is with passing params. Commented Dec 1, 2021 at 19:28

1 Answer 1

1

You could serialize the parameters yourself:

  1. Map each object into a string, containing 'ids=' prefixed to the object's id.
  2. Join the resulting array with a & delimiter.
const params = postRes.map(i => 'ids=' + i.id) 1️⃣
                      .join('&') 2️⃣
const getRes = await axios.get('employee/departments?' + params)

Run this snippet for example output:

const postRes = [{ id: 11 }, { id: 22 }, { id: 33 }]
const url = 'employee/departments?' + postRes.map(i => 'ids=' + i.id).join('&')
console.log(url)

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

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.