简体   繁体   English

正确的数据未到达 Twilio

[英]The right data is not getting to Twilio

I am trying to make an API call to Twilio with Go's HTTP package and it appears the right data is not getting to Twilio.我正在尝试使用 Go 的 HTTP package 对 Twilio 进行 API 调用,看起来正确的数据没有到达 Twilio。

Request:要求:

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
}

But Twilio is complaining is that:但是 Twilio 抱怨的是:

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

However, if I send the request from Postman, it works.但是,如果我从 Postman 发送请求,它就可以工作。

I am suspecting the "+" before the number is being stripped out but I am not sure as I am pretty new to Go and don't understand its nuances.我怀疑数字被删除之前的“+”,但我不确定,因为我对 Go 还很陌生,不了解它的细微差别。

Please, note that +1234567890 is just a dummy number I am using for this question.请注意, +1234567890只是我用于此问题的虚拟数字。

You probably want to use url.Values for this:您可能想为此使用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 https://play.golang.org/p/qAF3qlLIYPP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM