-
Notifications
You must be signed in to change notification settings - Fork 2
/
restTemplate.go
73 lines (66 loc) · 1.97 KB
/
restTemplate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
)
func main() {
/*
* Read your subscription key from an env variable.
* Please note: You can replace this code block with
* var subscriptionKey = "YOUR_SUBSCRIPTION_KEY" if you don't
* want to use env variables.
*/
subscriptionKey := os.Getenv("TRANSLATOR_TEXT_KEY")
if subscriptionKey == "" {
log.Fatal("Environment variable TRANSLATOR_TEXT_KEY is not set.")
}
/*
* Call our function to make a request. It takes a single argument
* the subscription key.
*/
translate(subscriptionKey)
}
func translate(subscriptionKey string) {
// Build the URL for our request (see: https://golang.org/pkg/net/url/#example_URL_Parse )
u, _ := url.Parse("https://api.cognitive.microsofttranslator.com/translate?api-version=3.0")
q := u.Query()
q.Add("to", "de")
q.Add("to", "it")
u.RawQuery = q.Encode()
/*
* Create an anonymous struct for our request body and encode it to JSON.
* This sample creates a JSON object per the Translator Text spec.
*/
body := []struct {
Text string
}{
{Text: "Hello, world!"},
}
b, _ := json.Marshal(body)
// Build the HTTP POST request
req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(b))
if err != nil {
log.Fatal(err)
}
// Add required headers
req.Header.Add("Ocp-Apim-Subscription-Key", subscriptionKey)
req.Header.Add("Content-Type", "application/json")
// Call the Translator Text API
res, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
// Decode the JSON response
var result interface{}
if err := json.NewDecoder(res.Body).Decode(&result); err != nil {
log.Fatal(err)
}
// Format and print the response to terminal
prettyJSON, _ := json.MarshalIndent(result, "", " ")
fmt.Printf("%s\n", prettyJSON)
}