0

I'm writing an server script with node.js which includes an request. I'm wondering if you could send an post request to local file like:

var request = require('request');
request({
    method: 'POST',
    url: '/v1/functions.php',
    formData: {
        'method': 'validate',
        'id': message.id,
        'token': message.token
    }
}, function(error, response, body) {
    if (error) {
        console.log(error);
    }

    console.log('response: '+ response.statusCode);
    console.log('body: '+ body);
});

But this is throwing an error:

Error: Invalid URI "/v1/functions.php"

It works if I use the full path:

http://localhost/something/something/v1/functions.php
Hene
  • 159
  • 3
  • 13

1 Answers1

1

No you cannot. Request states in their docs the url/uri must be fully qualified

uri || url - fully qualified uri or a parsed url object from url.parse()

Here is an answer describing a fully qualified URI

An alternative to read a local file is reading it using fs.readFile. Here is an example

var fs = require('fs');
fs.readFile('/functions.php', (err, data) => {
  if (err) throw err;
  console.log(data);
});
Andrew Lohr
  • 5,380
  • 1
  • 26
  • 38