Skip to content

Post a number as a JSON #2613

@slaout

Description

@slaout

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions