java - 将数据发布到谷歌文本到语音时收到的 Http 状态 400 接收到无效的 JSON 有效负载
问题描述
这是我使用 jsoup 发布 json 数据的代码
Response resp = Jsoup.connect("https://texttospeech.googleapis.com/v1beta1/text:synthesize")
.header("Authorization", " Bearer "+credential.getAccessToken())
.header("Content-Type", "application/json; charset=UTF-8")
.ignoreContentType(true)
.method(Method.POST)
.ignoreHttpErrors(true)
.requestBody("{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }")
.execute();
这是错误消息:
{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name \"{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }\": Cannot bind query parameter. Field '{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
{
"@type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"description": "Invalid JSON payload received. Unknown name \"{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets.\" }, \"voice\":{ \"languageCode\":\"en-gb\", \"name\":\"en-GB-Standard-A\", \"ssmlGender\":\"FEMALE\" }, \"audioConfig\":{ \"audioEncoding\":\"MP3\" } }\": Cannot bind query parameter. Field '{ \"input\":{ \"text\":\"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets' could not be found in request message."
}
]
}
]
}
}
我认为 json 字符串无效,但不是,我验证 json 字符串,并且它有效。
解决方案
检查 HTTP 标头
确保您通过以下方式发送请求:
内容类型:应用程序/json
推荐阅读
- algorithm - 与大O比较和对比
- html - 如何将 div 中的文本放置在图像旁边?
- css - React CSS 应用顺序
- testlink - 如何在 Testlink 上允许对已执行的测试用例进行需求版本链接管理
- flutter - 在 Flutter 中获取 Future var 的长度
- go - Textile Go - 通过 HTTP API 将文件添加到线程的描述不明确
- android - 使用 ConstraintLayout 中的圆形定位时如何设置按钮边距?
- python - 如何从熊猫数据框中的列中删除不必要的信息
- ios - Apple Seach Ads Discovery Campaigns 卡在等待受众规模验证
- c# - 在 C# 中创建对象和对象序列化