简体   繁体   中英

Curl corresponding request in node-fetch

It's been days I've been trying to make this curl request work with node-fetch without success.

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"

Thanks in advance for you help.

Note: I'm using React Native .

i don't know if Node.JS has a BETTER api to do this (but i'm surprised if it does not), i don't do Node.js programming myself, but i know how to do this with the normal browser javascript API XMLHttpRequest , i also know that Node.js support XMLHttpRequest.

the equivalent-ish XMLHttpRequest would be

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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