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.