node js post file to php api on remote server

I have to post a file to a php api url on a remote server. I am creating a http.request and then trying to write file data to this request by piping it from filestream as below :

var req = http.request( options, function(response) {
  response.on('data', function (chunk) {
console.log('BODY: ' + chunk);
 });
});
var fs = require('fs');
var boundaryKey = "5JtsIWpXzRoEIdPqkQiSWv4Tl8Bmq";//Math.random().toString(16); //     random string
var filelen = filelen; \\Already calculated before
req.setHeader('Content-Type', 'multipart/form-data;boundary='+boundaryKey);
req.setHeader('Content-Length', filelen);
// the header for the one and only part (need to use CRLF here)

req.write('--' + boundaryKey + '\r\n'
      // "name" is the name of the form field
      // "filename" is the name of the original file
  + 'Content-Disposition: form-data; name="gamefile"; filename="image.jpg"\r\n'
  // use your file's mime type here, if known
  + 'Content-Type: image/jpeg\r\n' 
  + 'Content-Transfer-Encoding: binary_file_data\r\n\r\n' 
);

var fileStream = fs.createReadStream('/image.jpg');
     fileStream.pipe(req, { end: false }); // maybe write directly to the socket here?
 fileStream.on('end', function() {
 console.log("File Streamed");
 // mark the end of the one and only part
 req.end('--' + boundaryKey + '--\r\n\r\n'); 
 });

On my php server what I get in files array :

Array                                                                                                                    
(                                                                                                                        
    [gamefile] => Array                                                                                                  
        (                                                                                                                
            [name] => image.jpg                                                                                          
            [type] =>                                                                                                    
            [tmp_name] =>                                                                                                
            [error] => 3                                                                                                 
            [size] => 0                                                                                                  
        )                                                                                                                

) 

which means that my file is not reaching the server. Please suggest if i am doing something wrong or do need to do something else.

Take a look at the Request module. It makes doing REST requests really easy including doing a POST request with a file included. Take a look at the Forms documentation for more information. Basically it's something like:

var request = require('request');

var r = request.post('http://service.com/upload')
var form = r.form()
form.append('my_field', 'my_value')
form.append('my_buffer', new Buffer([1, 2, 3]))
form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png'))
form.append('remote_file', request('http://google.com/doodle.png'))