I am trying to POST some data and a file from a Node.js application to a PHP script. I am unable to get the file from PHP side. The request goes through and I get 200 back but PHP thinks the $_FILES array is empty.
My code is
var options = {
headers: {
'Content-Type': 'multipart/form-data'
}
};
var postData = querystring.stringify(req.body);
var contentLength = postData.length;
options.body = postData;
if (req.files) {
options.files = { userfile: req.files.userfile };
}
options.headers['Content-Length'] = contentLength;
options.uri = appConfig.url + urlMapping[req.url];
options.method = req.method;
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.send(JSON.parse(body));
}
});
Data send to php api
{ headers: { 'Content-Type': 'multipart/form-data', 'Content-Length': 155 },
body: 'first_name=Raju&last_name=Mathai&email_address=rajumathai%40gm.com&mobile_number=9745342931&password=12345&state=Kerala&date_of_birth=12%2F12%2F1988&type=1',
files:
{ userfile:
{ fieldName: 'userfile',
originalFilename: 'man.png',
path: '/tmp/CPu0ul670BTtgYXKU07Je_2c.png',
headers: [Object],
size: 11288,
name: 'man.png',
type: 'image/png' } },
uri: 'http://192.168.1.102/byker-api-server/webservices/try_new_provider_sign_up.php',
method: 'POST' }
PHP code
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email_address = $_POST['email_address'];
$mobile_number = $_POST['mobile_number'];
$password = $_POST['password'];
$state = $_POST['state'];
$date_of_birth = $_POST['date_of_birth'];
$type = $_POST['type'];
$uploadDir = "./uploads/"; //Uploading to same directory as PHP file
$file = basename($_FILES['userfile']['name']);
I am using multiparty
for parsing form data