我正在尝试使用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/