-
-
Notifications
You must be signed in to change notification settings - Fork 11.6k
Post a number as a JSON #2613
Description
Hello,
When posting an object or array with axios, axios automatically sets the request content type to 'application/json', which is the common use case for APIs nowadays.
According to the README, "By default, axios serializes JavaScript objects to JSON.".
And according to the official JSON specification's right sidebar, a valid JSON stream can also be a primitive value (null, a boolean, a number or a string).
But axios.post(url, 42) will not send the request as content-type='application/json', but as 'application/x-www-form-urlencoded'.
For now, in our project, we are setting the content-type as 'application/json' manually when sending numbers or other primitives, and our server is able to parse this JSON without any explicit declaration.
Wouldn't it be better for axios to do this by default, as numbers are valid JSON, and axios states that it sends JSON by default?
I didn't file this as a feature request or a bug, as it may be intentional.
Or if it is not intentional, it may be difficult to change that behavior, as it would introduce a breaking change.
Best regards,
Sébastien.