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 example does not work as described

See original GitHub issue

The 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:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
ArturGrigiocommented, May 14, 2017

with jQuery, you can do this

var data = {
    firstName: 'Fred',
    lastName: 'Flintstone'
}
axios.post('API?'+jQuery.param(data))
3reactions
gtamborerocommented, Mar 7, 2018

Thanks LostKobrakai!

By default axios sends JSON data and if you receive POST data on PHP you need to configurate the header as urlencoded:

var config = {
headers: {
   'Content-Type': 'application/x-www-form-urlencoded',
}
};

axios.post('URL', { data1: this.data1, data2: this.data2 }, config)
        .then((response) => { ... })
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTTP Request Methods – Get vs Put vs Post Explained with ...
HTTP POST request​​ We use POST to create a new resource. A POST request requires a body in which you define the data...
Read more >
POST - HTTP - MDN Web Docs
The HTTP POST method sends data to the server. The type of the body of the request is indicated by the Content-Type header....
Read more >
Client on Node.js: Uncaught ReferenceError: require is not ...
throws the Uncaught ReferenceError: require is not defined. I was able to work around that by specifying Node.js integration as true when ...
Read more >
HTTP/1.1: Method Definitions
Since a server's communication options typically depend on the resource, the "*" request is only useful as a "ping" or "no-op" type of...
Read more >
Tutorial - Undergraduate Admissions - Penn State
View the Self-Reported Academic Record (SRAR) Tutorial as a playlist. Your Penn State application is not considered complete until Penn State has received ......
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