0

I'm loading a large amount of data into a Node array. Then, I'm passing that data to a PHP page like this:

const rows = res.data.values;
axios.post('./template.php', qs.stringify({rows}))
    .then((res) => {
        console.log(res.data);
        fs.writeFileSync('test.php', res.data, 'utf-8');
    })

Then, when I get to test.php, the $_POST variable is only the first 100 entries of my array I just passed.

I noticed when debugging and using console.log() to print the same array, I was getting something that said "... 19 more items". I was able to resolve that by calling this:

 util.inspect.defaultOptions.maxArrayLength = null;

At the top of my function. This worked great to stop the "... 19 more items" and now the full array displays with console.log(), however, it seems to have had no effect on my POST.

I'm stumped as to what I'm missing here -- does anyone know why my array is getting truncated when doing the POST? Thanks!

JToland
  • 3,630
  • 12
  • 49
  • 70
  • Can you show me the output of the endpoint (template.php) and what you got in test.php ? – Rakesh Mehta Sep 16 '22 at 16:17
  • 1
    post the `rows` as-is, see docs https://axios-http.com/docs/post_example what your doing is stringifying it into a URL param, which will get truncated due to https://stackoverflow.com/questions/7724270/max-size-of-url-parameters-in-get – Lawrence Cherone Sep 16 '22 at 16:21
  • @LawrenceCherone If I remove the stringify, my array is blank on the other end (in the php file). I'm sure I'm doing something extremely dumb, but any idea what offhand? I've tried it a few different ways (with the curly braces, without, etc) – JToland Sep 16 '22 at 16:29
  • @RakeshMehta test.php is fairly long, but I'm getting the full Array I sent, but it's just exactly 100 entries. The last 19 I expect are not there. Other than that, it looks exactly like what you'd expect. – JToland Sep 16 '22 at 16:30
  • 1
    change it too `axios.post('./template.php', {rows})`, then it will be in `$_POST['rows']`, or `json_decode(file_get_contents('php://input'), true)['rows']` – Lawrence Cherone Sep 16 '22 at 16:33
  • Thats the correct way, don't stringify the data, instead POST as RAW JSON and use the content type headers: { "Content-Type": "application/json" } – Rakesh Mehta Sep 16 '22 at 16:38
  • @LawrenceCherone That got me very close, but the array is just showing up as Array on the php side. I'm not getting it decoded/converted correctly somehow. – JToland Sep 16 '22 at 17:26
  • if you do `echo $_POST['rows']` it would show `Array`, use `print_r($_POST['rows'])` – Lawrence Cherone Sep 16 '22 at 17:37

1 Answers1

0

Something like this

   let objectData = [{a: 1, b: 2}, {a: 5, b:6}]; 
   axios.post(url, objectData, {
      headers: {
        "Content-Type": "application/json"
      }
    })

and at the PHP side, read the raw INPUT

$json = file_get_contents('php://input');
$data = json_decode($json);
Rakesh Mehta
  • 519
  • 2
  • 9
  • This is getting me close, by on the php page, I'm just getting the word "Array" for my data. Clearly it's not getting decoded right or something. – JToland Sep 16 '22 at 17:26