Home > Backend Development > Golang > Trying Twilio Whatsappp api call but getting error 20422

Trying Twilio Whatsappp api call but getting error 20422

WBOY
Release: 2024-02-08 20:48:09
forward
662 people have browsed it

尝试 Twilio Whatsappp api 调用但收到错误 20422

Question content

I encountered an error while using twilio Whatsapp api.

The following is the code:

package main

import (
    "fmt"

    "github.com/twilio/twilio-go"
    api "github.com/twilio/twilio-go/rest/api/v2010"
)

func main() {

    clientParameter := twilio.ClientParams{}
    clientParameter.Username = "AC***********************ba"
    clientParameter.Password = "ce************************27"
    clientParameter.AccountSid = "AC************************ba"

    client := twilio.NewRestClientWithParams(clientParameter)

    params := &api.CreateMessageParams{}
    params.SetContentSid("HT**********************70")
    params.SetMessagingServiceSid("MG******************************0d")
    params.SetFrom("whatsapp:+917*******2")
    params.SetTo("whatsapp:+917********4")

    resp, err := client.Api.CreateMessage(params)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        if resp.Sid != nil {
            fmt.Println(*resp.Sid)
        } else {
            fmt.Println(resp.Sid)
        }
    }
}
Copy after login

The error I receive is -

Status: 400 - ApiError 20422: Invalid Parameter (null) More info: https://www.twilio.com/docs/errors/20422
Copy after login

I get the same error if I try to do it via Postman.


Correct answer


Error20422 means that one of the three conditions is not met:

  • The specified Content-Type header field is missing
  • XML data is invalid or missing
  • Invalid parameter type or value

Since you are using the SDK, it is most likely the third bullet point. Is there a reason why you are using the MessagingServiceSid and From fields? I recommend updating the client to the latest version and running this script:

package main

import (
    "fmt"
    "log"
    "os"

    "github.com/joho/godotenv"
    "github.com/twilio/twilio-go"
    api "github.com/twilio/twilio-go/rest/api/v2010"
)


func main() {
    err := godotenv.Load()
    if err != nil {
            log.Fatal("Error loading .env file")
    }

    client := twilio.NewRestClient()

    params := &api.CreateMessageParams{}
    params.SetTo("whatsapp:"+os.Getenv("RECIPIENT_PHONE_NUMBER"))
    params.SetFrom(os.Getenv("TWILIO_MESSAGING_SERVICE"))
    params.SetContentSid(os.Getenv("CONTENT_SID"))

    _, err = client.Api.CreateMessage(params)
    if err != nil {
        fmt.Println(err.Error())
    } else {
        fmt.Println("Message sent successfully!")
    }
}

Copy after login

The above is the detailed content of Trying Twilio Whatsappp api call but getting error 20422. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template