JayaKrishna Namburu JayaKrishna Namburu - 2 months ago 22
Node.js Question

Handling multipart post method request in Nodejs throwing Unexpected Field Error

I am trying a request a

post
method from Android App using
Loopj Async Http Client
, and handling the request using Node Server which uses
Multer

By using the code Below

Step-1

public static void post(String url, File entity, AsyncHttpResponseHandler responseHandler) {
RequestParams params = new RequestParams();
try {
params.put("image",entity);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
client.post(getAbsoluteUrl(url), params, responseHandler);
}


The getAbsoluteUrl return the actual Url to post the request

Step-2 (Node Server)

router.post('/upload_image', function(req, res) {
console.log(req.files);
});


But, after receiving the request my node js server is throwing some error like this which i cannot Understand, After few hours of googling and searching for other option i am still clueless to solve the issue.

Here is the issue:

POST /upload_image 500 1537.367 ms - 2057
Error: Unexpected field
at makeError (/home/jkrishna/Documents/ProjectX/node_modules/multer/lib/make-error.js:12:13)
at wrappedFileFilter (/home/jkrishna/Documents/ProjectX/node_modules/multer/index.js:39:19)
at Busboy.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/lib/make-middleware.js:112:7)
at Busboy.EventEmitter.emit (events.js:106:17)
at Busboy.emit (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/lib/main.js:31:35)
at PartStream.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/lib/types/multipart.js:213:13)
at PartStream.EventEmitter.emit (events.js:95:17)
at HeaderParser.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:51:16)
at HeaderParser.EventEmitter.emit (events.js:95:17)
at HeaderParser._finish (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:68:8)
at SBMH.<anonymous> (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:40:12)
at SBMH.EventEmitter.emit (events.js:95:17)
at SBMH._sbmh_feed (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:95:16)
at SBMH.push (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/node_modules/streamsearch/lib/sbmh.js:56:14)
at HeaderParser.push (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/HeaderParser.js:46:19)
at Dicer._oninfo (/home/jkrishna/Documents/ProjectX/node_modules/multer/node_modules/busboy/node_modules/dicer/lib/Dicer.js:197:25)

Answer

The problem is that you've configured multer to look for a file whose field name is 'file', but in your Android code you're sending a field named 'image'. Change one or the other and it should work.

Comments