在节点js服务器中通过http请求接收二进制数据
Receiving binary data via http request in node js server
我正在尝试在节点 js Express 应用程序中接收图像二进制数据。数据由以下 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" 属性时,它工作得很好,但是这个方法不适用于其他属性。
我在两端都使用了 base64 编码,这对我来说非常有效。
我正在尝试在节点 js Express 应用程序中接收图像二进制数据。数据由以下 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" 属性时,它工作得很好,但是这个方法不适用于其他属性。
我在两端都使用了 base64 编码,这对我来说非常有效。