How to upload a file using

I am trying to upload an image file to a Rest server (a more specific variant of Confluence) using the Restify module, but getting an Assertion error. I'm not sure if I'm using the correct approach for uploading files to a REST server. Can anyone point me in the right direction?

This is my attempt -

var restify = require('restify');
var header = {'Authorization': 'Basic xxxxx', 'content-type': 'multipart/form-data'};
var client = restify.createJsonClient({
   url: 'http://www.testsite.com',
   version: '*',
   headers: header
});
var image = "c:\\Users\\abc\\Documents\\bbb.jpg";           
var fileStream = fs.createReadStream(image);
var stat = fs.statSync(image);
var size = stat["size"];
var param = "?pageId=123&filename=mynewimage&mimeType=image%2Fjpeg&size=" + size;       
fileStream.pipe(
    client.post("/plugins/drag-and-drop/upload.action"+param, function(err, req, res, obj) {
        if (err) {
            return err;
        }

    })
);  

      

UPDATE:

This is the assertion error I receive assert.js: 86

throw new assert.AssertionError ({
        ^
AssertionError: body
    at Object.module.exports. (anonymous function) [as ok] (c: \ Users \ abc \ myproj \ node_modules \ restify \ node_modules \ assert-pl
us \ assert.js: 242: 35)
    at JsonClient.write (c: \ Users \ abc \ myproj \ node_modules \ restify \ lib \ clients \ json_client.js: 31: 12)
    at ReadStream.ondata (_stream_readable.js: 540: 20)
    at ReadStream.emit (events.js: 107: 17)
    at readableAddChunk (_stream_readable.js: 163: 16)
    at ReadStream.Readable.push (_stream_readable.js: 126: 10)
    at onread (fs.js: 1683: 12)
    at FSReqWrap.wrapper [as oncomplete] (fs.js: 529: 17)
+3


source to share


2 answers


To post multipart/form-data

, you will have to use a library other than restify

, because it doesn't support that Content-Type

.



You can use request

which supports multipart/form-data

HTTP requests.

0


source


I was trying to send a request containing multipart/form-data

and process that request in the API with restify 5

. Following @mscdex's answer , I came up with the following code that handles both scenarios:

"use strict"

const restify  = require('restify'),
  plugins  = require('restify-plugins'),
  fs       = require('fs'),
  request  = require('request'),
  FormData = require('form-data');

var server = restify.createServer();
server.use(plugins.multipartBodyParser());  // Enabling multipart

// Adding route
server.post('/upload', (req, res, next) =>{
    /**
    * Processing request containing multipart/form-data
    */
    var uploaded_file = req.files.mySubmittedFile;  // File in form

    //Reading and sending file
    fs.readFile(uploaded_file.path, {encoding: 'utf-8'}, (err, data)=>{
        // Returning a JSON containing the file name and its content
        res.send({
            filename: uploaded_file.name,
            content: data
        });
        next()
    });
});

// Launching server at http://localhost:8080
server.listen(8080, start);

// Client request
function start(){
    // Making a request to our API using form-data library
    // Post file using multipart/form-data
    var formData = {
        mySubmittedFile: fs.createReadStream('/tmp/hello.txt')
    }
    request.post({'url': 'http://localhost:8080/upload', formData: formData}, function(err, res, body){
        console.log("Response body is: "+ body);
    });
}

      

Using:



  • form-data@2.2.0
  • request@2.81.0
  • restify@5.0.1
  • restify-plugins@1.6.0
  • node @ 6.11.1

I tried not using libraries form-data

and request

only using restify / http, but I ended up with awful long code.

0


source







All Articles