首页 文章

Postman/Insomnia 中缺少 PUT API 体

提问于
浏览
1

我正在使用 Postman 和 Insomnia 等应用程序发送 API 请求。

我可以使用 form-data 的 Body 发送 POST 请求,以便它看起来像

parameters:[{"fullname" : "John Smith", "username" : "jsmith", "email" : "jsmith@example.com", "password" : "s348nfdf"},{"fullname" : "Janine Smith", "username" : "jsmith1", "email" : "jsmith1@example.com", "password" : "ddfgret567"}]↵

一切正常,我可以从后端的请求中获取参数。

但是,如果我将请求更改为 PUT,则主体(上述参数)不在请求($_REQUEST)中。请求本身成功,没有错误代码。

我也尝试了 x-www-form-urlencoded 和原始 JSON。

为什么身体(参数)缺失?

1 回答

  • 1

    $_REQUEST[]中没有正文。使用:

    $body = file_get_contents('php://input');

    来自PHP 手册输入 I/O streamsdocs:

    php://input 是一个 read-only 流,允许您从请求正文中读取原始数据。在 POST 请求的情况下,最好使用 php://input 而不是$HTTP_RAW_POST_DATA,因为它不依赖于特殊的 php.ini 指令。此外,对于默认情况下未填充$HTTP_RAW_POST_DATA 的情况,它可能是激活 always_populate_raw_post_data 的内存密集型替代方案。 php://input 不适用于 enctype =“multipart/form-data”。

相关问题