JSON 编码的请求是什么样的?

发布于 2024-08-31 11:45:17 字数 115 浏览 1 评论 0原文

我正在使用 PHP 构建 API。

我需要做什么才能识别 JSON 编码的请求?

它是否带有特定的请求类型?

如何获取请求正文以便可以对其进行 json_decode?

I'm building an API using PHP.

What do I need to do to recognize a JSON encoded request?

Does it come with a certain request type?

How do I get just the request body so that I can json_decode it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-09-07 11:45:17

RFC 4627 建议使用 mime 类型 application/json。您可以使用 php://input 读取请求数据。

RFC 4627 recommends the mime type application/json. You can read the request data with php://input.

我的影子我的梦 2024-09-07 11:45:17

json 的 mime 类型是 application/json,但我怀疑它是否已经流行起来并将其添加到响应中。

发现 json 响应的最简单方法是它很可能用花括号括起来,并在每个成员后面有冒号。但这仍然不是一个确定它是 json 的方法。

从 PHP 测试它的最简单方法是尝试对其进行 json_decode,如果它抛出错误,则要么不是 json,要么不是格式良好的 json,因此您将无法使用它。

The mime type for json is application/json, but I doubt that has caught on yet to add that to responses.

The easiest way to spot a json response is that it will most likely be wrapped in curly braces and have colons after each member. But that still isn't a sure fire way to know it's json.

The easiest way to test it from PHP is to try to json_decode it, if it throws an error, either it's not json, or it's not well-formed json and thus you won't be able to use it anyway.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文