0

So I have a call I can make to an api that allows me to toggle whether or not a user has a vip role using this endpoint.

ApiService.patch('/admin/vip?user_id=1&user_id=2')

It can target multiple users at once by appending "user_id=:id" after the question mark with a "&" in between each users id. How could I make a variable of some kind that uses a loop to get the correct user ids and apply "user_id=" before their actual IDs and "&" between each user?

I can handle looping to get the IDs but not sure how to put "user_id=" before each id and "&" after each ID (Only if there is another user_id after the last one)

albert_anthony6
  • 594
  • 2
  • 9
  • 30

2 Answers2

1

I'd say the easiest way to achieve this is by having a comma separated value for user_id. In your code you could then do something like query.user_id.split(',') and get an array of all the user IDs you passed in.

  • Could you give an example of this? Query would be undefined in my case – albert_anthony6 Jul 28 '20 at 17:41
  • I have to clarify that this would only work if you had access to modifying the API endpoint. If not, I'd say that taplar's solution may work. But, in reality, I feel like the API is kind of broken since it's taking the same query param many times, which really shouldn't happen. Most web frameworks parse query params, and those are unique. – Alejandro Corredor Jul 28 '20 at 17:43
  • There are more talks about this here: https://stackoverflow.com/questions/1835074/sending-http-request-with-multiple-parameters-having-same-name – Alejandro Corredor Jul 28 '20 at 17:45
0

Use an array to store ids and then map the array to create the string, using a condition to skip the first. Or use URL Search Params