登录
首页 >  Golang >  Go问答

GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API

来源:stackoverflow

时间:2024-02-25 22:30:27 229浏览 收藏

欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!

问题内容

我使用以下代码进行翻译:

import (
    "context"

    "cloud.google.com/go/translate"
    "golang.org/x/text/language"
    "google.golang.org/api/option"
)

func translatefromgoogle(sourcearr []string, srclangcode, trgtlangcode, apikey string) (targetarr []translate.translation, err error) {
    src, err := language.parse(srclangcode)
    if err != nil {
        return targetarr, err
    }
    trgt, err := language.parse(trgtlangcode)
    if err != nil {
        return targetarr, err
    }
    ctx := context.background()
    opts := option.withapikey(apikey)
    c, err := translate.newclient(ctx, opts)
    if err != nil {
        return targetarr, err
    }
    defer c.close()
    targetarr, err = c.translate(ctx, sourcearr, trgt,
        &translate.options{
            source: src,
            format: translate.text,
        })
    return targetarr, err
}

在错误中,返回以下类型的数据:

googleapi: got http response code 400 with body: \n\n  \n  \n  error 400 (bad request)!!1\n  \n  \n  

400. that’s an error.\n

your client has issued a malformed or illegal request. that’s all we know.\n

但我只想要这样的文本:

Your client has issued a malformed or illegal request.

是我遗漏了什么还是 sdk 问题?如何获得所需的消息作为输出?


正确答案


该方法应该是解析错误输出并从中获取所需的消息。

package main

import (
    "fmt"
    "strings" 
    "bytes"
    
    "golang.org/x/net/html"
)

func main() {

    var s []string 
    
    string := "googleapi: got http response code 400 with body: \n\n  \n  \n  error 400 (bad request)!!1\n  \n  \n  

400. that’s an error.\n

your client has issued a malformed or illegal request. that’s all we know.\n" doc, err := html.parse(strings.newreader(string)) if err != nil { fmt.println(err) } var f func(*html.node) f = func(n *html.node) { if n.type == html.elementnode && n.data == "p" { text := &bytes.buffer{} collecttext(n, text) s = append(s, text.string()) } for c := n.firstchild; c != nil; c = c.nextsibling { f(c) } } f(doc) fmt.println(s[1]) // return value } func collecttext(n *html.node, buf *bytes.buffer) { if n.type == html.textnode { buf.writestring(n.data) } for c := n.firstchild; c != nil; c = c.nextsibling { collecttext(c, buf) } }

输出:

your client has issued a malformed or illegal request.  that’s all we know.

更新:要解释为什么应使用上述方法,您必须检查“翻译”功能的工作原理:

func (c *client) translate(ctx context.context, inputs []string, target language.tag, opts *options) ([]translation, error)

因此,如您所见,它返回翻译类型和错误类型:

// translation
type Translation struct {
    Text string
    Source language.Tag
    Model string
} 

// error
type error interface {
    Error() string
}

查看更多here

总之,您必须解析错误字符串才能获得所需的输出。

以上就是《GO SDK 返回 HTML 而不是 JSON 用于 Google Translation API》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>