[GS7] How to validate Postal Code with Geocoding and Search API

Use Case

You may want to validate postal code per known city and country names.
This article include the solution to the above use case with one incorrect implementation reported from a support ticket.

Solution

The solution to the above use case is as follows, and the key point is applying the query parameter "&types=postalCode":
https://geocode.search.hereapi.com/v1/geocode?qq=city=GAHANNA;postalCode=43230∈=countryCode:USA&apikey=`{{your_api_key}}`&types=postalCode
For invalid postal codes other than 43230, the above query will return an empty items list as "items": [] so it can validate the input postal code.

Alternative

When the query parameter "&types=postalCode" is not included, the HERE GS7 API will ignore the incorrect postal code input.
In such a case, a "localityType": "city" record will be returned, and you need to compare the postal code input value with its value in response.

Example

As an example which was also reported by a support ticket, the city GAHANNA and postal code 1122 were used for validation.
The expected result is validation false because 1122 is not correct, but because "&types=postalCode" was not applied it passed the validation.
There are two solutions:

one is adding "&types=postalCode" and having an empty response for invalid postal code input
or compare the returned postal code with the input value to validate if they are equal (pass) or not (fail)