Created
May 3, 2016 04:14
-
-
Save IndianGuru/4a01096827c05bf1f07ae23bcd3eb805 to your computer and use it in GitHub Desktop.
Send text to get a .wav file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
) | |
func main() { | |
url := fmt.Sprintf("https://api.api.ai/v1/tts?v=20150910&text=The+Go+programming+language+rocks!") | |
// Create file to store the .wav file | |
output, err := os.Create("d.wav") | |
if err != nil { | |
log.Fatal("Create: ", err) | |
return | |
} | |
defer output.Close() | |
// Build the request | |
req, err := http.NewRequest("GET", url, nil) | |
if err != nil { | |
log.Fatal("NewRequest: ", err) | |
return | |
} | |
// Replace Your_Client_Token with your actual token | |
req.Header.Add("Authorization", "Bearer Your_Client_Token") | |
req.Header.Add("Accept-language", "en-US" ) | |
// For control over HTTP client headers, | |
// redirect policy, and other settings, | |
// create a Client | |
// A Client is an HTTP client | |
client := &http.Client{} | |
// Send the request via a client | |
// Do sends an HTTP request and | |
// returns an HTTP response | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal("Do: ", err) | |
return | |
} | |
// Callers should close resp.Body | |
// when done reading from it | |
// Defer the closing of the body | |
defer resp.Body.Close() | |
if _, err := io.Copy(output, resp.Body); err != nil { | |
log.Fatal("Copy: ", err) | |
return | |
} | |
fmt.Println("File d.wav downloaded.") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment