POST example does not work as described
See original GitHub issueThe POST example on the main page causes issues on a basic Apache install.
Using the example as is, the data is encoded in json and is not extracted automatically by the web server (I’m using PHP in this case). It is received as:
console> axios.post(url, {first_name: "Fred", last_name: "Flintstone"});
# from apache error log for <?php error_log(var_export($_REQUEST, true)); ?>
array (
'{"first_name":"Fred","last_name":"Flintstone"}' => ''
)
In jQuery, for instance, running something similar would be encoded properly:
console> $.post(url, {first_name: "Fred", last_name: "Flintstone"});`
// from apache error log for <?php error_log(var_export($_REQUEST, true)); ?>
array (
'first_name' => 'Fred',
'last_name' => 'Flintstone',
)
Even in the post examples, within server.js, the data has to be unencoded from json (which may be the standard for Node, but it would be nice to have things work out of the box).
The config docs state that you can pass in a plain object, FormData, etc, but they are not handled the same. Or there is a default encoding or transform that is happening that is not documented.
After hitting a brick wall for a few hours, I was able to figure out a way to make it work using FormData…but if this was documented, this would be great:
var data = new FormData();
data.append("first_name", "Fred");
data.append("last_name", "Flintstone");
axios.post(url, data);
// from apache error log for <?php error_log(var_export($_REQUEST, true)); ?>
array (
'first_name' => 'Fred',
'last_name' => 'Flintstone',
)
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (1 by maintainers)
Top GitHub Comments
with jQuery, you can do this
Thanks LostKobrakai!
By default axios sends JSON data and if you receive POST data on PHP you need to configurate the header as urlencoded: