简体   繁体   English

如何在REQUEST nodejs中返回正文

[英]How to return body in REQUEST nodejs

I just want to return a body but I get only {} instead real data.我只想返回一个主体,但我只得到 {} 而不是真实数据。

const Ocr = (file,req,response) => {

    const options = {
        method: "POST",
        url: "https://api.aiforthai.in.th/ocr",
        headers: {
            "Apikey": "LgArg8PNY2BiY1cmtFsE1XXN6bP6O903",
            "Content-Type": "multipart/form-data"
        },
        formData: {
            "uploadfile": fs.createReadStream(file)
        }
    };

    request(options, function (err, res, body) {
        if (err) {
            console.log(err)
            return 
        }
        return body //here I want to return
    });

};

And this is my main function that call the top code.这是我调用顶级代码的主要 function。

exports.testOcr = async (req, res, next) => {
    try {
        const file = "docs/uploadsNotice/65/กรุงเทพ.jpg"
        const result = await requestOcr.Ocr(file)
        return res.status(200).json({
            data: result,
        });
    } catch (error) {
        next(error);
    }
};

My function is not returning body我的 function 没有返回正文

Can someone please help me with this?有人可以帮我吗?

Are you sure you are using json body-parser as a middle-ware, if not, there won't be a body on req.body .你确定你正在使用 json body-parser 作为中间件吗,如果不是, req.body上不会有body

you can install body-parser via npm: npm install body-parser你可以通过 npm 安装 body-parser: npm install body-parser

and then use the middle-ware as shown below:然后使用如下所示的中间件:


    const bodyParser = require("body-parser");
    app.use(bodyParser.json())

for more information check the official express docs here .有关更多信息,请在 此处查看官方快递文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM