简体   繁体   中英

Passing array with key-value in and REST API following HAL

I'm developing a REST API based on the HAL specification. The API is being developed in PHP with Symfony and the client is being build in angularjs. The API must return some lists and they are going to be shown using ngtables library , and filtering options will be needed, ideally in the format that ngtables uses, like in this sample .

 { "name": "M", "age": "4" } 

(Of course future clients may be use the same options.)

My frontend-workmate is suggesting me to send the json encoded directly as a parameter in the url, like this:

?filter=%7B%20%22name%22%3A%20%22M%22%2C%20%22age%22%3A%20%224%22%20%7D%20

I'm not very sure about that though.

I've been reading how to send arrays through the URL with a parameter like param[]=foo&param[]=bar but that does implies have one key for multiple values. In my case I need to be able to send different key-values for the arrays like a Json object does.

I'm wondering how should be the Request URL from the client to be able to include all the filter options inside one parameteror and if there is any standard way to do that.

EDITED : After @Ross_Turner I want to clarify that my intention of passing a key-value array inside a parameter is because we'll have another type of parameter. For example:

?filter=<my key-value array>&page=1&limit=50&anotherParam=whatever

Even we would like to use another parameter containing another key-value array like this:

?filter=<my key-value array>&sorting=<another key-value array>&page=1&limit=50

Where filter and sorting can have the same keys.

Currently there is no standard way to send queries in GET requests. So you have 2 options: you use your custom query language (like you already tried) or you choose a standard query language. Either way it is hard to make it RESTful, because you have to create a link, which contains a recipe how to generate such an URI. You can probably use URI templates , but sometimes they are not general enough...

@Ross Turner:

I think you misunderstood, I was talking about not having a standard. Ofc. there are non standard query string based solutions, for example RQL . Or you can send a standard SPARQL query in a single param, but there is no standard way to describe such a link, and without link descriptions you cannot generate the query in a REST client, so it won't be loosely coupled and so it will violate the uniform interface constraint.

I disagree with @inf3ero's answer - surely there is a very well defined way to send a query as part of a GET request - query parameters. If you want different key and value combinations this is the principal of the keys and values of query parameters, so in your example you should probably use ?name=M&age=4 and parse each of these on your server. It should be simple enough to convert

{ "name": "M", "age": "4" } 

into this by iterating over the keys in the JSON object. I'm not sure why you would want to combine these into a single parameter if you are attempting to implement a RESTful API following the HAL specification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM