NodeJS exec -在运行curl而不是API resonse时获取进度数据[重复]

2hh7jdfx  于 2023-08-04  发布在  Node.js
关注(0)|答案(1)|浏览(133)

此问题在此处已有答案

How do I get cURL to not show the progress bar?(7个答案)
How to get cURL to output only HTTP response body (JSON) and no other headers etc(2个答案)
6天前关门了。
我尝试使用下面的代码在exec中运行curl。

const { exec } = require('child_process');

const shell = (cmd, errMsg) => {
    return new Promise((resolve, reject) => {
        exec(cmd, (error, stdout, stderr) => {
            if (error || stderr) {
                console.error(error || stderr);
                reject(errMsg ?? (error || stderr));
            }
            resolve(stdout);
        })
    })
}

const test = async (curlCmd) => {
    try {
        const stdout = await shell(curlCmd, 'curl failed');
        console.log(stdout);
    } catch (err) {
        console.error(err);
    }
}

test('curl -X GET "https://dummyapi.com/get-data?p1=abc&p2=def&" -H "Cache-Control: no-cache" -H "Content-Type: application/json"')

字符串
但是并没有得到API响应,而是返回了一些进度数据,如下所示

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   188  100   188    0     0    274      0 --:--:-- --:--:-- --:--:--   276


如果我在终端中点击相同的命令,我会得到正确的响应。
节点版本:14.19.1

6ljaweal

6ljaweal1#

添加-s标志以隐藏进度条:

test('curl -s -X GET "https://dummyapi.com/get-data?p1=abc&p2=def&" -H "Cache-Control: no-cache" -H "Content-Type: application/json"')

字符串

相关问题