【问题标题】:Curl corresponding request in node-fetch在 node-fetch 中 curl 对应的请求
【发布时间】:2019-07-17 06:45:19
【问题描述】:

这几天我一直试图让这个 curl 请求与 node-fetch 一起工作,但没有成功。

curl -X GET 
-u "<username>:<password>" 
--output hello_world.mp3 
"https://gateway-lon.watsonplatform.net/text-to-speech/api/v1/synthesize/?accept=audio/mpeg&text=hello"

提前感谢您的帮助。

注意:我正在使用 React Native。

【问题讨论】:

  • 你写的代码是什么?它会抛出什么错误?或者它是如何不起作用的

标签: node.js react-native curl node-fetch


【解决方案1】:

我不知道 Node.JS 是否有一个 BETTER api 来执行此操作(但如果没有,我会感到惊讶),我自己不做 Node.js 编程,但是我知道如何使用普通浏览器 javascript API XMLHttpRequest 来做到这一点,我也知道 Node.js 支持 XMLHttpRequest。

等效的 XMLHttpRequest 将是

{
    let xhr = new XMLHttpRequest();
    xhr.open("GET", "https://gateway-lon.watsonplatform.net/text-to-speech/api/v1/synthesize/?accept=audio/mpeg&text=hello", true);
    xhr.responseType = "arraybuffer";
    xhr.setRequestHeader("Authorization", "Basic " + btoa("<username>:<password>"));

    xhr.onload = function(ev) {
        fs.appendFile("hello_world.mp3", xhr.response, {
            flag: "wb"
        });
    };
    xhr.send();
}

【讨论】:

    猜你喜欢
    • 2022-01-24
    • 2021-05-22
    • 2019-06-22
    • 2020-11-11
    • 1970-01-01
    • 2021-07-20
    • 1970-01-01
    • 2018-05-22
    • 1970-01-01
    相关资源
    最近更新 更多