1

My params in React is an array, checkedItems:['location1', 'location2']. When it calls my API, the URL looks like this

/api/search?checkedItems[]=location1&checkedItems[]=location2 

instead of

/api/search?checkedItems=['location1','location2']

My API application is expecting to query a comma delimited array parameter:

  public List<myclass> Get( string checkedItems = null)
    {
      IQueryable<myclass> qry = (from a in db.myclass
                       select a);

        if (checkedItems != null)
        {


            string[] items = checkedItems.Split(',');
            foreach (var item in items)
            {

                {
                    qry = qry.Where(a => items.Contains(a.mycolumn));

                }

            }
           }

     return qry.ToList();
       }

How do I change the react code or change API code? Thanks.

2 Answers 2

7

Instead of '/api/search?checkedItems=['location1','location2']', separate the array field with commas, like so '/api/search?checkedItems=location1,location2'

To do this,

let checkedItems = ['location1', 'location2']
// using template literals
let url = `/api/search?checkedItems=${checkedItems.join(',')}`

Then on the backend simply get the query param checkedItems and split it checkedItems.split(',') to get the array.

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

Comments

0

This largely depends on your backend technology, but a lot of API libs/framework automatically transform parameters to array for you.

Keep in mind that having an URL like /api/search?checkedItems=location1&checkedItems=location2 (so: same parameter multiple times) is a perfectly valid URI.

I can't say if this is your case or not.

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.