3 Replies - 272 Views - Last Post: 13 February 2020 - 12:38 PM Rate Topic: -----

#1 omgwalt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 20

Data posted from Vue app using Axios does not populate $_POST

Posted 13 February 2020 - 09:34 AM

I'm working on an app in Vue/Quasar using Axios to post a request to a simple PHP authentication script on a remote server via an https connection which serves as a simple middleware to a back-end on the server.

While the data does arrive at the server, for some reason it doesn't populate the $_POST array in PHP. Instead, it just arrives as a string in php://input like this:

{"username":"test","password":"Thisisatest!"}

How can I get my app's post request to properly populate $_POST on the remote server so I can process it in my PHP script?

Is This A Good Question/Topic? 0
  • +

Replies To: Data posted from Vue app using Axios does not populate $_POST

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2494
  • View blog
  • Posts: 7,551
  • Joined: 15-January 14

Re: Data posted from Vue app using Axios does not populate $_POST

Posted 13 February 2020 - 10:41 AM

If you really want that in $_POST then you need to put it there yourself. PHP doesn't do that automatically because PHP expects data to be in the form-data format, and that is JSON. The normal way to handle that is just to get the request body from php://input and use json_decode to turn it into an array or object.
Was This Post Helpful? 0
  • +
  • -

#3 omgwalt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 20

Re: Data posted from Vue app using Axios does not populate $_POST

Posted 13 February 2020 - 11:26 AM

Thank you, ArtificialSoldier. For anyone looking at this in the future:

<?php
if(!$_POST['username'] && !$_POST['password']) {
	$data = file_get_contents("php://input");
	$post = json_decode($data,true);
	$_POST['username'] = $post['username'];
	$_POST['password'] = $post['password'];
}
?>


Was This Post Helpful? 0
  • +
  • -

#4 omgwalt   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 10-February 20

Re: Data posted from Vue app using Axios does not populate $_POST

Posted 13 February 2020 - 12:38 PM

More abstracted answer:

<?php
if(!$_POST) {
	$data = file_get_contents("php://input");
	$post = json_decode($data,true);
	foreach( $post ?? [] as $key => &$value ) {
		$_POST[$key] = $value;
	}
	unset($value);
}
?>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1