lundi 1 mars 2021

Add xml buffer to form data

I want to upload a XML-file with superagent (Protractor Testresults, so it's inside NodeJS context). I use 'fs' to read the XML-file with readFileSync which returns a Buffer Object for the XML file. Unfortunately I don't know how i can append this Buffer to the formData for the Request. If i do it like this, I just get an error: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type object

    //Imports and var declaration
    var FormData = require('form-data');
    var fs = require('fs');
    
    const xmlFile = "\\junit_xml_generation\\file.xml";
    var formData = new FormData();    
    
    //Read File
    fs.readFileSync(__dirname + xmlFile, function(err, data) {
      
      //Add To Form Data
      formData.append('file', Buffer.from(data, 'utf-8'), {filename: 'file.xml'});

      //Request
      superagent
        .post("http://myurl.com")
        .set("Content-Type", "multipart/form-data")
        .type('xml')
        .send(formData)
        .end(function(err, res){
          console.log("Result: ", res, err);
        });
    });

I have tried so much, but I don't know how I can covert this to Buffer/ArrayBuffer so the request doesn't give me the error of incompatible types...




Aucun commentaire:

Enregistrer un commentaire