The problem of how to send post by buffer in Node.js

send the buffer to the backend through the http.request in Node.js. How can the server get these datanies?

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();

    })
}

where data is buffer type data, above is sending data playback, and the backend receiver is Django, code as follows:

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"])

the above code is the back-end processing code that I used to convert data of buffer type to string and send it to the backend through JSON.stiringify. Now, I would like to ask all code friends, if you send data of type buffer directly, how should the backend get the data sent?

Mar.09,2021
Menu