目录
一、以火山翻译为例子
1.完整代码
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
// "strings"
"os"
)
type DictResponse struct {
Words []struct {
Source int `json:"source"`
Text string `json:"text"`
PosList []struct {
Type int `json:"type"`
Phonetics []struct {
Type int `json:"type"`
Text string `json:"text"`
} `json:"phonetics"`
Explanations []struct {
Text string `json:"text"`
Examples []struct {
Type int `json:"type"`
Sentences []struct {
Text string `json:"text"`
TransText string `json:"trans_text"`
} `json:"sentences"`
} `json:"examples"`
Synonyms []interface{} `json:"synonyms"`
} `json:"explanations"`
Relevancys []interface{} `json:"relevancys"`
} `json:"pos_list"`
} `json:"words"`
Phrases []interface{} `json:"phrases"`
BaseResp struct {
StatusCode int `json:"status_code"`
StatusMessage string `json:"status_message"`
} `json:"base_resp"`
}
type DictRequest struct {
Text string `json:"text"`
Language string `json:"language"`
}
func query(word string){
client := &http.Client{}
// var data = strings.NewReader(`{"text":"hello","language":"en"}`)
request := DictRequest{Text: word, Language: "en"}
buf, err := json.Marshal(request)
if err != nil {
log.Fatal(err)
}
var data = bytes.NewReader(buf)
req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVLQDcCwINsSW/NZM9WX7jy&_signature=_02B4Z6wo000015fhpVAAAIDDF-NfE7sRI6-X4aHAAIeXgfG9te8myjutgShQB8jqUDatCy40MOwV-0bqoPUXu9GDqeo.aQKg0Wf1bxIiw8tkAhWWJy3ifEthBdCgN69zoNwnBh2B5hsm-2NKb1", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "translate.volcengine.com")
req.Header.Set(&