节点获取 PUT 失败 - 错误的“连接:关闭”处理?

时间:2021-03-05 10:35:28

标签: javascript node.js api fetch node-fetch

首先,让我们在浏览器中执行:

fetch('https://cdn.nodesite.eu/static/89a85d082e363a84a32454cfeced61c165da744975f47df0c04d0fa1bd19df05/put', {
    method: 'PUT',
    headers: {
        Authorization: 'Bearer YWxhZGRpbjpvcGVuc2VzYW1l',
        'Content-Type': 'text/plain',
        'Content-Length': 4,
        'Content-Encoding': 'none',
    },
    body: 'test',
}).then(async r => {
    for (const a of r.headers.entries()) {
        console.log(a);
    }
    console.log([await r.text()])
});

我们得到了正确的回应。

["89a85d082e363a84a32454cfeced61c165da744975f47df0c04d0fa1bd19df05"]

现在让我们在 Node.js 中做同样的事情

const fetch = require('node-fetch');
fetch('https://cdn.nodesite.eu/static/89a85d082e363a84a32454cfeced61c165da744975f47df0c04d0fa1bd19df05/put', {
    method: 'PUT',
    headers: {
        Authorization: 'Bearer YWxhZGRpbjpvcGVuc2VzYW1l',
        'Content-Type': 'text/plain',
        'Content-Length': 4,
        'Content-Encoding': 'none',
    },
    body: 'test',
}).then(async r => {
    for (const a of r.headers.entries()) {
        console.log(a);
    }
    console.log([await r.text()])
});

相反,我们得到

[ '' ]

后端的相关部分是:

header_remove();
header('200 OK');
header('Content-Type: text/plain');
header('Content-Length: 64');
header('Content-Encoding: none');
header('Connection: close');
header('X-VR: Success');
print $h;
flush();

我怀疑问题在于 node-fetch 没有考虑 Content-Length 如果它收到 Connection: Close。但我不知道。有什么见解吗?

0 个答案:

没有答案