-1

Is there any standard as for sending amounts in rest ?

What is the proper way to send POST request with body with amount field and where can I find explanation ?

"amount": "2.222222222"

or

"amount": 2.222222222
michealAtmi
  • 1,012
  • 2
  • 14
  • 36
  • 2
    It depends on what API you're using. The API could be written to accept `amount` as string or as integer, the latter would make more sense, but it should be written in the API documentation for the API you're using. – Mark Oct 19 '18 at 11:51
  • Please fix the post title according to the content. Now it is confusing. And try to explain better your issue, because I'm confused about the mixing of REST with JSON format. – Mario Santini Oct 19 '18 at 11:52
  • Speacking of POST request, they require a body, where you should put the query parameters instead of placing in the URL. The POST body could be a normal URL parameters: key=value separated by & it is not required to be a JSON. But as @Mark wrote this depends on the API. – Mario Santini Oct 19 '18 at 11:54
  • About JSON the second example is more appropriate, but again, this depends on the API that can handle float values with strings. – Mario Santini Oct 19 '18 at 11:56

1 Answers1

0

Note that REST isn't always JSON. You appear to be actually asking about JSON-encoding money values.

If you're consuming an existing JSON API, you don't have a choice to make: the API spec will tell you whether the field is expected to be a string or a number.

If you're designing a new API, you need to make this choice.

The JSON standard specifies how to encode a number containing a decimal point, but it doesn't specify anything about how the decoded number should be represented. It's very likely that a decoding library would decode it as a floating-point number.

With very few exceptions, it's a bad idea to use floating-point for money: Why not use Double or Float to represent currency?. For this reason my advice would be use strings to transfer money values, and explicitly convert in both the client and the server. This means leaves less scope for intermediate JSON handling code to change the value.

Also note that a JSON Number has no unit. If you encode a monetary value as a string, you have the option to include a currency in the format: "£123.45". Of course both the encoder and the decoder would need to know about this format.

slim
  • 40,215
  • 13
  • 94
  • 127