Node.js中的buffer如何发送post问题

通过Node.js中的http.request将buffer发送到后端,请问服务器端如何获取这些数据尼?

function  doapost(data,i) {
    return new Promise(function (resolve,reject) {
        let contents = data
        data = null;
        let options = {
            host: "localhost",
            path: "/nodepost/",
            port: 8000,
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Content-Length': contents.length
            }
        };
        let req = http.request(options, function (res) {
            console.log("第"+i+"份请求返回数据")
            res.on("data", function (chunk) {
                console.log(chunk.toString());
            });
            res.on("end", function (d) {
                resolve("end");
            });
            res.on("error", function (e) {
                reject(e);
            })
        });
        req.write(contents);
        req.end();

    })
}

其中data为buffer类型数据,上面是发送数据放,后端接收方为Django,代码如下:

def nodepost(request):
    username=request.POST['username']
    chunks = dict(
        data=bson.binary.Binary(bytes(request.POST['data'])),
        n=request.POST['n'],
        file_id=request.POST['file_id'],
        username=username,
        length=request.POST['length']
    )
    client = pymongo.MongoClient('localhost', 27017)
    db = client.cloudfiledb
    db[username + "filedata"].insert(chunks);
    return HttpResponse(chunks['username'])

上面的代码是之前我将buffer类型的数据通过JSON.stiringify转换为字符串发送过去后后端的处理代码,现在请问各位码友,如果直接发送buffer类型的数据,后端应该如何获取发送过来的数据尼?

阅读 2.2k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题