HMT
HMT

Reputation: 2261

how to fetch local currency code from api url?

The API I am trying to write should provide support for getting the fares in local currency. How should I extract the country currency code ?

I will be using java currency api for conversions

Upvotes: 0

Views: 787

Answers (1)

moritz.vieli
moritz.vieli

Reputation: 1807

Usually, you detect the location (= country) you got from the browser/IP. e.g:

Based on this information, you can then apply the language, currency, formatting etc.

You should provide a way to override this location (e.g. a drop down for the user), because it's sometimes not detected correctly or the user may want to use a different one, because he's travelling.

It's not a bad idea to store the current location in the URL. Often, such URLs look like this:

www.acme.com/us/xyz

Therefore, the URL www.acme.com would detect the location automatically and forward the user to the default one (e.g. www.acme.com/us), where the user would be able to overwrite it.

You can afterwards query your REST API with HTTP GET like this, where USD is derived from the location:

GET .../api/price?currency=USD

If you need to use a POST anyway, because you have many parameters, you might also provide the requested currency in the body, although it's not very RESTish. The advantage of having all parameters in the URL is, you may also benefit from caching mechanisms and it's everywhere clearly visible, what you were requesting (e.g. in the logs). HTTP headers are usually used for security-information (should not be visible in the logs) and values used in every request (e.g. Accept, etc.).

Upvotes: 1

Related Questions