question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Post a number as a JSON

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
McNeticcommented, Jan 15, 2020

There are certainly use cases for this; it’s perfectly valid to send primitives as JSON body. I just encountered the same problem.

As for the urgency; it is not that urgent as there is a workaround in available and it is a breaking change; however, according to the docs, axios should (by default) encode objects to JSON. This is obviously broken for primitive objects, and leads to unexpected behavior.

In fact, the concept to switch to application/x-www-form-urlencoded by simply using qs.stringify() is logically flawed, as it contradicts the default encoding to JSON for strings. I think this is a design flaw that should be adressed prior to releasing a 1.0.0.

1reaction
yasufcommented, Dec 20, 2019

that’s part of the default transformRequest, looks like it was on purpose, set the content-type as application/x-www-form-urlencoded, from our defaults.js. It’s weird as you mention that it’s set to form-urlencoded, if we make the change it would have to be on the 1.0.0 release, since this is a breaking change for some use cases. Doesn’t seem urgent unless you can describe a use case for this, we have a few things to fix before this one. Thanks for looking into this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to send Integer value in the form of JSON format and ...
Just send the number in JSON. 12345 use @RequestBody to receive the int number. it will work. I use postman to send it...
Read more >
JSON Data Types - W3Schools
In JSON, values must be one of the following data types: a string; a number; an object (JSON object); an array; a boolean;...
Read more >
Numeric types — Understanding JSON Schema 2020-12 ...
There are two numeric types in JSON Schema: integer and number. They share the same validation keywords. JSON has no standard way to...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
JSON.stringify() converts a value to JSON notation representing it: Boolean , Number , String , and BigInt (obtainable via Object() ) objects ...
Read more >
JSON Data Types - REST
At the granular level, JSON consists of 6 data types. The first four data types (string, number, boolean and null) can be referred...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found