0

I am using Slim framework to create an REST API for my project. The WebApp itself is developed using ReactJS.

I have a doubt regarding the same. I have a set of values stored in an array in my ReactJS App. These values are filters which is used ti filter data from my table.

My table contains a field called location. In my WebApp I have search filter were I can add multiple locations based on which the results have to be displayed. I store these filters in an array.

I want to know how can I pass this array to Rest API Route so that I can get this array in my REST API Framework.

I want to achieve something like

/routes/location/[arrayOfValues]

6
  • Why not /routes/location?val[]=abc&val[]=def Commented Mar 4, 2018 at 4:34
  • secure.php.net/manual/en/function.serialize.php ? Commented Mar 4, 2018 at 4:35
  • @BrianPutt I havent tried that yet. Will it work because I havent seen that anywere else..? Commented Mar 4, 2018 at 4:37
  • @nogad this is good if am able to receive the data in the framework. I am not able to send the data to framework from my Reactjs WebApp Commented Mar 4, 2018 at 4:38
  • It'll work if you look for queryParams since you're passing the data in via a get request. But you may want to look at doing a POST request instead due to urlencoding, etc...issues that may arise. Commented Mar 4, 2018 at 4:38

1 Answer 1

2

There are only two ways of passing data to the server - either as URL params (for POST, GET, or anything else) or in the request body (for PUT or POST). Are you also writing the code to handle the API request? From your description of the use case, it sounds like GET would be the most semantically correct verb, so you you should use URL params as @BrianPutt suggested. Using square brackets is the standard way to encode an array value. How are the locations defined? Longitude/latitude? If so, you could also just build a comma-separated string, e.g.

/routes/location?q=12.3409,-40.0004,2.34004,120.0294

But odds are your back end framework has built-in support for parsing those URL params, so you should just use that.

/routes/location?q[]=12.3409,-40.0004&q[]=2.34004,120.0294

The only reason not to do this is if you have a huge number of locations, in which case you might want to use a POST request and build some kind of JSON object to put in the body.

Also: be consistent! If the rest of your API uses JSON, you should probably use it here too.

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

1 Comment

yes.. I am writing code handle api request. The location is defined as names, not coordinates. I do use JSON to return the result from api to WebApp.

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.