Reputation: 61
On a front end device I have OpenCV running with Python. I then send a computed image to a server for further processing. My problem is I can't decode the image with Express.
I send an image from this front end device thusly (in Python 2.7):
import requests
url = "http://10.1.10.194:3000"
files ={'image':open('testimg.jpg','rb')}
r = requests.post(url,files=files)
This successfully sends a post command to my Express app, which hears the post request and does the following:
app.post('/', function(request, respond) {
console.log(request.headers)
var image = request.body
fs.writeFile('test.jpg', image, function(err){
if (err) throw err
console.log('File saved.');
});
respond.end('thanks');
});
I know I'm correctly receiving the picture, as the header correctly lists the filesize, but when I print my image, it's simply a text file [Object object].
Output of the Express app is here:
{ host: '10.1.10.194:3000',
'content-length': '14551',
'accept-encoding': 'gzip, deflate',
accept: '*/*',
'user-agent': 'python-requests/2.9.1',
connection: 'keep-alive',
'content-type': 'multipart/form-data; boundary=e664d9584c484962bfe4d1577bd4d91b' }
POST / 200 15.754 ms - -
File saved.
My Express app is successfully loading body-parser, but I can't seem to figure out how to get my raw data out of the request.body. Any advice is welcome. Thanks!
Upvotes: 0
Views: 2370
Reputation: 36
Did you try using Multer module ?
var multer = require('multer');
var upload = multer({dest: './uploads/'});
app.post('/', upload.single('file'), function(request, respond) {
if(request.file) console.log(request.file);
}
https://github.com/expressjs/multer
Upvotes: 2
Reputation: 1389
You need to wait for post data to be extracted.
if (request.method == 'POST') {
var body = '';
request.on('data',function(data) { body += data; });
request.on('end', function(data) {
fs.writeFile('test.jpg', body, function(err){ ... })
});
Upvotes: 0