使用 Twilio 通过 POST 发送消息,但不接受 JSON 或 XML 形式的正文,只有 To、From 和 Body 的普通参数有效 [英] Using Twilio to send Messages via POST, but will not accept the body as JSON or XML, only plain parameters of To, From and Body works

查看:23
本文介绍了使用 Twilio 通过 POST 发送消息,但不接受 JSON 或 XML 形式的正文,只有 To、From 和 Body 的普通参数有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的平台迫使我使用 JSON 格式或 XML 格式的主体发送 POST 请求,因此为了测试我使用的是 SOAPUI,因此我可以准确指定测试所需的主体.我正在发布到 URLhttps://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxxxxxxxxxxx/Messages.json

The platform I am using forces me to send POST requests with a body of JSON format or XML format, so to test I am using SOAPUI so I can specify exactly the body I need for testing. I am POSTing to the URL https://api.twilio.com/2010-04-01/Accounts/ACxxxxxxxxxxxxxxxxxxxx/Messages.json

具有 JSON 正文

{
    "Body": "hi dear",
    "To": "+1631xxxxxxx",
    "From": "82xxx"
}

但它会产生错误

{"code": 21604, "message": "A 'To' phone number is required.", "more_info": "https://www.twilio.com/docs/errors/21604", "status": 400}

但是如果 SOAP 主体是简单的参数

but if the SOAP body is simple parameters

To=%2B1631xxxxxxx&From=82xxx&Body=Hi%20Dear

消息发送正确,我得到了 Twilio 的响应

The message is send correctly and I get the Twilio response of

{
    "sid": "SMxxxxxxxxxxxxxxxxxxxxx",
    "date_created": "Tue, 06 Oct 2020 03:00:01 +0000",
    "date_updated": "Tue, 06 Oct 2020 03:00:01 +0000",
...
}

Twilio 是否有消息 API,我可以使用格式化的 JSON 或 XML 正文进行 POST.

Does Twilio have a messages API that I can POST with a formatted JSON or XML body.

推荐答案

Twilio 开发人员布道者在这里.

Twilio developer evangelist here.

Twilio API 只接受正文格式为 application/x-www-form-urlencoded 或您在帖子中提到的简单参数的请求.这是向 Twilio API 发出请求的唯一方法.

The Twilio API only accepts requests with the body formatted as application/x-www-form-urlencoded or simple parameters as you mention in your post. That is the only way to make requests to the Twilio API.

核心 API 将通过附加相关后缀(.json.csv)以 XML(默认情况下)或可选的 JSON、CSV 和 HTML 进行响应或 .html).较新的 API,例如 productname.twilio.com/v1 等子域下的任何内容,仅以 JSON 响应.

The core APIs will respond with XML (by default) or optionally JSON, CSV and HTML by appending the relevant suffix (.json as you have included, .csv or .html). Newer APIs, anything under a subdomain like productname.twilio.com/v1, only respond with JSON.

Twilio 还提供了 6 种不同语言的帮助库,并且有社区维护的库用于许多不受支持的语言,应该抽象出请求的格式.

Twilio also provides helper libraries in 6 different languages, and there are community maintained libraries for many unsupported languages, that should abstract away the format of the request.

让我知道这是否有帮助.

Let me know if that helps at all.

这篇关于使用 Twilio 通过 POST 发送消息,但不接受 JSON 或 XML 形式的正文,只有 To、From 和 Body 的普通参数有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆