如何把 go 请求的 api 接口,翻译为 curl ?

动作开发 · 970 次浏览
Poto 创建于 2021-08-17 13:52

https://www.volcengine.com/docs/4640/65066

请将该请求表述为 curl 

package main

import (
    "fmt"
    "net/http"
    "net/url"
    "time"

    "github.com/volcengine/volc-sdk-golang/base"
)

const (
    kAccessKey      = {密钥管理中的 AccessKey ID}
    kSecretKey      = {密钥管理中的 AccessKey Secret}
    kServiceVersion = "2020-06-01"
)

var (
    ServiceInfo = &base.ServiceInfo{
        Timeout: 5 * time.Second,
        Host:    "open.volcengineapi.com",
        Header: http.Header{
            "Accept": []string{"application/json"},
        },
        Credentials: base.Credentials{Region: base.RegionCnNorth1, Service: "translate"},
    }
    ApiInfoList = map[string]*base.ApiInfo{
        "LangDetect": {
            Method: http.MethodPost,
            Path:   "/",
            Query: url.Values{
                "Action":  []string{"LangDetect"},
                "Version": []string{kServiceVersion},
            },
        },
    }
)

func newClient() *base.Client {
    client := base.NewClient(ServiceInfo, ApiInfoList)
    client.SetAccessKey(kAccessKey)
    client.SetSecretKey(kSecretKey)
    return client
}

func main() {
    client := newClient()
    resp, code, err := client.Json("LangDetect", nil, `{"TextList":["你好世界"]}`)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d %s\n", code, string(resp))
}

回复内容
CL 2021-08-17 13:55
#1

这个最好对方直接有API接口的文档描述应该怎么传递http参数。

自己猜SDK内部如何添加参数,太费劲了,除非整个sdk的代码读一遍。

回复主贴