我正在尝试在节点js express app中接收图像二进制数据。数据由以下python脚本发送:
import requests
import shutil
from time import sleep
def uploadFile(url,filename):
url = 'http://208.97.169.108:3000'
files = {'photo':open("C:/EmojifyMe/downloadedImage.jpg",'rb')}
values = {'name':"Ali"}
unsplashAuthHeader = {"auth":unsplash_id,'acceptversion': 'v1'};
response = requests.post(url,files=files);
print(response.text)
if(response.status_code == 200):
print("Upload Successful")
python脚本发出的HTTP请求是:
{
"args": {},
"data": "",
"files": {
"photo": "[image binary data comes here]"
},
"form": {
"sentby": "Osama Asif"
},
"headers": {
"Accept": "/",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"auth":"14fe25847dcac2578100192",
"Content-Length": "273",
"Content-Type": "multipart/form-data; boundary=59e0880732fd417bbca2fefe900275de",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.4"
},
"json": null,
"origin": "103.255.4.58",
"url": "http://httpbin.org/post"
}
这是我到目前为止在节点JS服务器上尝试获取属性“files.photo”时所做的:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post('/', urlencodedParser, function(req, res)
{
res.writeHead(200);
console.log("Received");
console.log(req.body.files.photo);
res.end("Got it!")
});
app.listen(3000);
console.log("Listening to port 3000");
收到请求后,控制台会记录“undefined”。
我正在寻找一种方法来接收正确的二进制数据,并将其转换回jpg图像。
P.S当我尝试使用req.get(“auth”)访问“headers”的“auth”属性时,它工作正常,但此方法不适用于其他属性。
答案 0 :(得分:0)
我在两端使用了base64编码,这对我来说非常合适。