Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
we use request() and superagent() method for uploading files from node js to server via url, it cannot upload the file and shows below error, the following code superagent method is:


JavaScript
 request.post('/upload')
.attach('image', path.join(__dirname +'/new.txt')
).end(function(err, res){if(err){console.log(err);
}
else
{
console.log(res);
}
});


below is error for superagent()
   text: '<?xml version="1.0" encoding="UTF-8"?>\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"\n  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\n<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">\n<head>\n<title>Object not found!</title>\n<link rev="made" href="mailto:you@example.com" />\n<style type="text/css"><!--/*--><![CDATA[/*><!--*/ \n    body { color: #000000; background-color: #FFFFFF; }\n    a:link { color: #0000CC; }\n    p, address {margin-left: 3em;}\n    span {font-size: smaller;}\n/*]]>*/--></style>\n</head>\n\n<body>\n<h1>Object not found!</h1>\n<p>\n\n\n    The requested URL was not found on this server.\n\n  \n\n    If you entered the URL manually please check your\n    spelling and try again.\n\n  \n\n</p>\n<p>\nIf you think this is a server error, please contact\nthe <a href="mailto:you@example.com">webmaster</a>.\n\n</p>\n\n<h2>Error 404</h2>\n<address>\n  <a href="/">localhost</a><br />\n  <span>Apache/2.4.29 (Unix) OpenSSL/1.0.2m PHP/5.6.32 mod_perl/2.0.8-dev Perl/v5.16.3</span>\n</address>\n</body>\n</html>\n\n',
   method: 'POST',
   path: '/upload' },
accepted: false,
noContent: false,
badRequest: false,
unauthorized: false,
notAcceptable: false,
forbidden: false,
notFound: true,
type: 'text/html',
charset: 'utf-8',
links: {},
setEncoding: [Function: bound ],
redirects: [] } }


What I have tried:

<pre>and the other method by request()

JavaScript
var jsonUpload = {  };
//console.log(data);
//var jsonUpload = data;
var formData = {
    'feedback': fs.createReadStream(path.join(__dirname, "/feedback.json")),
    'jsonUpload': JSON.stringify(jsonUpload),
};
console.log(formData.feedback);
var uploadOptions = {
    "url": "https://testlink.jsp",
    "method": "POST",
    "headers": {
        'Authorization': 'apikey',
        //'X-forwarded-for':'1.2.3.4',
        //'contentType': 'application/json',
    },
    "formData": formData
}
var req = request(uploadOptions, function(err, resp, body) {
    if (err) {
        console.log('Error ', err);
    } else {
        console.log(body);
    }
});


below is their error:

endEmitted: false, reading: false, sync: true, needReadable: false, emittedReadable: false, readableListening: false, resumeScheduled: false, defaultEncoding: 'utf8', ranOut: false, awaitDrain: 0, readingMore: false, decoder: null, encoding: null }, readable: true, domain: null, _events: { end: [Function] }, _eventsCount: 1, _maxListeners: undefined, path: 'C:\xampp\htdocs\dac\json_push_service\feedback.json', fd: null, flags: 'r', mode: 438, start: undefined, end: undefined, autoClose: true, pos: undefined, bytesRead: 0 } Server Created {"ERROR":"No feedback data available","STATUS":"COMPLETE WITH ERROR"}>
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900