go - 正确的数据无法传递到Twilio

标签 go twilio go-http

我正在尝试使用Go的HTTP包对Twilio进行API调用,并且似乎正确的数据没有到达Twilio。
要求:

func startVerification() (string, error) {
    url := fmt.Sprintf("https://verify.twilio.com/v2/Services/%s/Verifications", internal.Config.TwilioServiceSID)
    xx := "Locale=es&To=+1234567890&Channel=sms"
    payload := strings.NewReader(xx)

    log.Println(fmt.Sprintf("Payload = %v", xx))
    client := &http.Client{}
    req, err := http.NewRequest("POST", url, payload)

    if err != nil {
        return "", fmt.Errorf("error occured while creating request %v", err)
    }
    req.SetBasicAuth(internal.Config.TwilioSD, internal.Config.TwilioAuthToken)
    req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

    res, err := client.Do(req)
    if err != nil {
        return "", fmt.Errorf("error occured while doing %v", err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    if err != nil {
        return "", fmt.Errorf("error occured while reading %v", err)
    }
    return string(body), nil
}
但是Twilio提示的是:

{ "code": 60200, "message": "Invalid parameter To: 1234567890", "more_info": "https://www.twilio.com/docs/errors/60200", "status": 400 }


但是,如果我从 postman 发送请求,则它可以工作。
我怀疑数字被删除之前的“+”号,但我不确定Go还是很陌生,也不了解它的细微差别。
请注意,+1234567890只是我用于此问题的虚拟数字。

最佳答案

您可能要为此使用url.Values:

params := url.Values{}
params.Add("Locale", "es")
params.Add("To", "+1234567890")
params.Add("Channel", "sms")
payload := strings.NewReader(params.Encode())
https://play.golang.org/p/qAF3qlLIYPP

关于go - 正确的数据无法传递到Twilio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64545047/

相关文章:

Go - 如何加载新的 html 表单

go - Go 中通过和失败的测试用例列表

http - Go http 客户端超时与上下文超时

go - 什么时候在 golang 中使用劫持?

php - Twilio 从网站向多个用户发送短信

go - web.Request 结构的字段状态未在 Golang 中正确克隆

go - 传入请求:具有自定义类型字段的上下文

recursion - Go 中的短变量声明和长变量声明之间的闭包范围区别是什么?

twilio - Twilio 现在支持子流吗?

twilio - 获取 Twilio session 的持续时间