4

I need to use python to send an image through post and then download it on the node.js server side.

Python code:

import requests
from PIL import Image
import json

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, data = files)

Node.js code:

var app = express();
app.use(bodyparser.json({ limit: '50mb' }));
app.use(bodyparser.urlencoded({ limit: '50mb', extended: true }));

app.post('/ay', function(req, res) {
    var base64Data = req.body.file
    require("fs").writeFile("out.png", base64Data, 'base64', function(err) {
        console.log(err);
    });

    res.send('done');
});

But I can't seem to download the file properly on the server so I'm wondering what format python uses to open images and how I can fix the node.js code so that it can properly download the image.

Edit: there were a few issues with the code, I'm trying to use multer now but can't seem to get it working.

Python code:

import requests

url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files = files)

Node.js code:

var express = require('express');
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();

app.post('/ay', upload.single('avatar'), function (req, res, next) {
    console.log(req.file)
    res.send("done");
});

app.post('/ay', upload.array('photos', 12), function (req, res, next) {
    console.log(req.files)
    res.send("done");
});

I've tried both upload.single and upload.array but neither work.

user1883614
  • 905
  • 3
  • 16
  • 30
  • Have you included middleware to handle the POST data in Node ? – adeneo Mar 02 '16 at 18:34
  • Yup! I just edited my code to show it. I am receiving the image on the server side but I don't know how it's encoded so I can't seem to decode it. – user1883614 Mar 02 '16 at 18:37
  • bodyParser does not work on files, it says so right on the top of the [documentation](https://github.com/expressjs/body-parser). You need multer, busboy or one of the others that support files. Also, files has to be sent as `multipart/form` with the correct keys, not JSON. – adeneo Mar 02 '16 at 18:42
  • And I think your python should be `r = requests.post(url, files=files)` – adeneo Mar 02 '16 at 18:48

2 Answers2

3

So I finally figured it out using multer... incorrectly naming the key is why I couldn't use multer properly.

Python:

import requests
url = 'http://127.0.0.1:8080/ay'
files = {'file': open('image.jpg', 'rb')}
r = requests.post(url, files = files)

Node.js:

var express = require('express');
var multer  = require('multer')
var upload = multer({ dest: 'uploads/' })
var app = express();

app.post('/ay', upload.array('file', 12), function (req, res, next) {
    console.log(req.files)
    res.send("done");
});
user1883614
  • 905
  • 3
  • 16
  • 30
0

Have a look at this blog post which gives an example regarding how to access an uploaded file in node.js

In that example, after you load the bodyParser middleware, you have access to an object called req.files which contains the info regarding your uploaded file.

Do a console.log(req.files) and see what it displays.

The bodyParser middleware can be used to read uploaded files in Express v3, which is no longer maintained.

If you use v4 or above, you can use the connect-multiparty middleware, like this:

var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.post('/ay', multipartMiddleware, function(req, resp) {
  console.log(req.body, req.files);
  // don't forget to delete all req.files when done 
});

Also, I think your Python code is not uploading properly. Try with:

requests.post('http://127.0.0.1:8080/ay', files={'image.jpg': open('image.jpg', 'rb')})
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • So I have the nodejs version of that already working... but this is python to nodejs which is why I'm running into this issue. My json that is sent to the server only has 1 key-value pair which is file:some encoding. – user1883614 Mar 02 '16 at 18:42
  • how do you know that the node part works OK? do you have an HTML form that uploads a file to that endpoint and it's read correctly? – Tudor Constantin Mar 02 '16 at 18:51
  • Yup, that's what I had. I did test your code... the req.body - how do I convert that back into an image? Right now I'm seeing alphanumeric characters and % in my terminal. – user1883614 Mar 02 '16 at 21:02
  • Actually, I edited the code - now I'm trying to use multer instead of your multipart (because that code did work) but I can't get it to work. – user1883614 Mar 02 '16 at 21:20