我正在嘗試使用 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 抱怨的是:{“代碼”:60200,“消息”:“無效參數To:1234567890”,“更多信息”:“https://www.twilio.com/docs/errors/60200”,“狀態”:400 }但是,如果我從 Postman 發送請求,它就可以工作。我懷疑數字被刪除之前的“+”,但我不確定,因為我對 Go 很陌生,不了解它的細微差別。請注意,這+1234567890只是我用于此問題的虛擬數字。
1 回答

慕姐4208626
TA貢獻1852條經驗 獲得超7個贊
你可能想用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
- 1 回答
- 0 關注
- 89 瀏覽
添加回答
舉報
0/150
提交
取消