这是我参与「第三届青训营 -后端场」笔记创作活动的的第一篇笔记
一、网络抓包
1)在彩云词典界面打开 f12 开发者模式
2)开发者模式窗口切换到networks
3)在彩云翻译中键入中文并翻译
4)找到对应翻译的请求dict,右击复制其cURL
二、代码转换
1)将复制得来的 cURl 粘贴到 “curl command” 窗口
2)复制得到的 GO 代码
三、代码解读(见注释)
import ( "fmt" "io/ioutil" "log" "net/http" "strings")func main() { //未限制时间 client := &http.Client{} //将字符串转化为流,只读且高效 var data = strings.NewReader(`{"trans_type":"en2zh","source":"fluent"}`) //发起请求 req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data) if err != nil { log.Fatal(err) } //设置一堆请求头 req.Header.Set("Accept", "application/json, text/plain, */*") req.Header.Set("Accept-Language", "en-GB,en;q=0.9,en-US;q=0.8,zh-CN;q=0.7,zh;q=0.6") req.Header.Set("Connection", "keep-alive") req.Header.Set("Content-Type", "application/json;charset=UTF-8") req.Header.Set("DNT", "1") req.Header.Set("Origin", "https://fanyi.caiyunapp.com") req.Header.Set("Referer", "https://fanyi.caiyunapp.com/") req.Header.Set("Sec-Fetch-Dest", "empty") req.Header.Set("Sec-Fetch-Mode", "cors") req.Header.Set("Sec-Fetch-Site", "cross-site") req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.54 Safari/537.36 Edg/101.0.1210.39") req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi") req.Header.Set("app-name", "xy") req.Header.Set("os-type", "web") req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="101", "Microsoft Edge";v="101"`) req.Header.Set("sec-ch-ua-mobile", "?0") req.Header.Set("sec-ch-ua-platform", `"Windows"`) //发起请求 resp, err := client.Do(req) if err != nil { log.Fatal(err) } //函数结束后自动关闭流,防止内存泄露 defer resp.Body.Close() //阅读响应,将流转为byte数组存于内存 bodyText, err := ioutil.ReadAll(resp.Body) if err != nil { log.Fatal(err) } //打印json字符串 fmt.Printf("%s\n", bodyText)}
代码执行后得到如下的json
{"rc":0,"wiki":{},"dictionary":{"prons":{"en-us":"[\u02c8flu\u0259nt]","en":"[\u02c8flu\u0259nt]"},"explanations":["a.\u6d41\u7545\u7684;\u6d41\u5229\u7684"],"synonym":[],"antonym":["faltering","stammering"],"wqx_example":[],"entry":"fluent","type":"word","related":[],"source":"wenquxing"}}
四、生成request body
1)修改个结构体,其成员与json元素一一对应
type DictRequest struct { TransType string `json:"trans_type"` Source string `json:"source"` UserID string `json:"user_id"`}
2)代码替换
request := DictRequest{TransType: "en2zh", Source: word} buf, err := json.Marshal(request) if err != nil { log.Fatal(err) } var data = bytes.NewReader(buf)
替换
var data = strings.NewReader(`{"trans_type":"en2zh","source":"fluent"}`)
五、解析response body
1)利用代码解析工具将json转为 go 结构体
2)修改代码
err = json.Unmarshal(bodyText, &dictResponse) if err != nil { log.Fatal(err) } fmt.Printf("%#v\n", dictResponse)
将以上代码替换掉打印json的语句
六、修改修改字典输出
上一部会以json格式输出所有的信息,我们只需说明需要输出的部分
fmt.Println(word, "UK:", dictResponse.Dictionary.Prons.En, "US:", dictResponse.Dictionary.Prons.EnUs) for _, item := range dictResponse.Dictionary.Explanations { fmt.Println(item) }
并添加当报文返回值不是200是的错误提示命令
if resp.StatusCode != 200 { log.Fatal("bad StatusCode:", resp.StatusCode, "body", string(bodyText)) }
七、结果
输入中文你好,出现其英文翻译,成功!!!
原文:https://juejin.cn/post/7095572243748487199