0

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 ?

  • using accept-language header ? (In this case if my language is en , how should I fetch the currency code ?)
  • or should I pass the code in the url ?
  • or should I fetch the locale ? What will be the best approach ?

I will be using java currency api for conversions

HMT
  • 2,093
  • 1
  • 19
  • 51

1 Answers1

1

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.).

moritz.vieli
  • 1,747
  • 1
  • 14
  • 17
  • I don't have a lot of experience with web APIs but shouldn't it also be possible to detect the currency from the locale? I thought locale information was typically send by a browser (but might not be sent by default over say REST or something). – markspace Sep 18 '18 at 05:46
  • Ah, maybe I misunderstood you. To query different currencies over a REST API, a query parameter would definitely be correct. E.g. .../api/price?currency=USD. I updated my answer. – moritz.vieli Sep 18 '18 at 06:05
  • Added some more information on where to use query params, HTTP headers or even params in bodies. – moritz.vieli Sep 18 '18 at 06:12