API Workshop - Url Structure
The query component contains non-hierarchical data that, along with data in the path component, serves to identify a resource within the scope of the URI's scheme and naming authority (if any).
The query component is indicated by the first question mark ("?") character and terminated by a number sign ("#") character or by the end of the URI.
The characters slash ("/") and question mark ("?") may represent data within the query component.
http://api.walmartlabs.com/v1/search?query=chromebook&format=json&apiKey=AKey12345
The query string here start after the ? character
So in this query you have the following pairs that are separated by the & delimiter:
query=chromebookformat=jsonapiKey=AKey12345
When a HTML Form is submitted the content of the form is encoded as follows:
field1=value1&field2=value2&field3=value3...
The query string is composed of a series of field-value pairs.
Within each pair, the field name and value are separated by an equals sign, '='.
The series of pairs is separated by the ampersand, '&' (or semicolon, ';' for URLs embedded in HTML and not generated by a <form>...</form>.
When doing a filtering action with a query string parameter than think about using a unique query parameter
For instance to get a particular student from a list:
GET /students?id=5A simple sorting example can be illustrated here:
GET /users?sort=idHere we sort a list of users by ID
If we wanted to have a more advanced sorting feature we could perhaps do the following:
GET /users?sort=-id,gradeHere we have a comma separated list and use the - to denote descending sort order and additionally sort by grade
At times you need more powerful constructs so you could use something like Elastic search or any other technology based on Apache Lucene that does full text search.
curl -XGET 'localhost:9200/_search?q=logged'
Notice here that we use q=logged for our search query with Elastic Search.
The API consumer doesn't always need the full information returned from a particular resource. Mobile clients can especially take advantage of this feature if you provide it.
You can use a fields query parameter which takes a comma separated list.
GET /students?fields=id,name,gradeThis query will only return the fields: id, name, and grade
| Previous | Next |
|---|---|
| ← URI Design | API Design → |