Slim v4 - 无法从 PUT 请求中获取正文

发布于 2025-01-15 05:32:12 字数 1863 浏览 0 评论 0原文

我正在尝试使用 Slim 路由执行 PUT 请求。

我使用 POST 请求执行此操作没有问题,但当我尝试使用 PUT 执行此操作时,变量 $body (在下面的示例中)结果为“NULL”。 另外,通过变量 $args['id'] 我可以从 url 获取参数“id”。

    $app->put('/networks/{id}', function ($request,$response,$args) {
        $body = $request->getParsedBody();
        var_dump($body,1); 
        die();
        [...]
    });

请求标头

PUT /mysite/networks/1 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NDc2MTkzMDgsImV4cCI6MTY0NzYyMTEwOCwianRpIjoiNTg5MzNiZmY3ZDQwZmY2NmU4YmJhMjA0YzM1MDVkMTMiLCJpZCI6IjIwIiwiZW1haWwiOiJhZG1pbkBnbWFpbC5jb20iLCJtYXRyaWNvbGEiOiJNTVNEQSJ9.WBLAwfxlcT2vXTQugxYbYgSSb_XNaBREWXxTXSGSsdQ
X-Requested-With: XMLHttpRequest
Content-Length: 81
Origin: http://localhost
Connection: keep-alive
Referer: http://localhost/anas-ta/public/index.html
Cookie: _pk_id.1.1fff=0fb970783be6ee60.1646993471.; jwt_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NDc2MTkzMDgsImV4cCI6MTY0NzYyMTEwOCwianRpIjoiNTg5MzNiZmY3ZDQwZmY2NmU4YmJhMjA0YzM1MDVkMTMiLCJpZCI6IjIwIiwiZW1haWwiOiJhZG1pbkBnbWFpbC5jb20iLCJtYXRyaWNvbGEiOiJNTVNEQSJ9.WBLAwfxlcT2vXTQugxYbYgSSb_XNaBREWXxTXSGSsdQ
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin

这是使用标头“x-www-form-urlencoded”发送的示例请求正文:

name=test&streets%5B1%5D=A01%2CA02%2CA03&streets%5B2%5D=A01%2CA02%2CA03

...我对其进行了美化以提高可读性:

{
    "name": "test",
    "streets[1]": "A01,A02,A03",
    "streets[2]": "A01,A02,A03"
}

感谢支持 C:

I'm trying to do a PUT request with a Slim route.

I'have no problem doing it with a POST request, but when i try to do it with PUT, the variable $body (in the example below) results "NULL".
Also by the variable $args['id'] i can get the parameter "id" from the url.

    $app->put('/networks/{id}', function ($request,$response,$args) {
        $body = $request->getParsedBody();
        var_dump($body,1); 
        die();
        [...]
    });

The request headers

PUT /mysite/networks/1 HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:98.0) Gecko/20100101 Firefox/98.0
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: it-IT,it;q=0.8,en-US;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NDc2MTkzMDgsImV4cCI6MTY0NzYyMTEwOCwianRpIjoiNTg5MzNiZmY3ZDQwZmY2NmU4YmJhMjA0YzM1MDVkMTMiLCJpZCI6IjIwIiwiZW1haWwiOiJhZG1pbkBnbWFpbC5jb20iLCJtYXRyaWNvbGEiOiJNTVNEQSJ9.WBLAwfxlcT2vXTQugxYbYgSSb_XNaBREWXxTXSGSsdQ
X-Requested-With: XMLHttpRequest
Content-Length: 81
Origin: http://localhost
Connection: keep-alive
Referer: http://localhost/anas-ta/public/index.html
Cookie: _pk_id.1.1fff=0fb970783be6ee60.1646993471.; jwt_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE2NDc2MTkzMDgsImV4cCI6MTY0NzYyMTEwOCwianRpIjoiNTg5MzNiZmY3ZDQwZmY2NmU4YmJhMjA0YzM1MDVkMTMiLCJpZCI6IjIwIiwiZW1haWwiOiJhZG1pbkBnbWFpbC5jb20iLCJtYXRyaWNvbGEiOiJNTVNEQSJ9.WBLAwfxlcT2vXTQugxYbYgSSb_XNaBREWXxTXSGSsdQ
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin

This is an example request body sended with header "x-www-form-urlencoded":

name=test&streets%5B1%5D=A01%2CA02%2CA03&streets%5B2%5D=A01%2CA02%2CA03

...i prettyed it for readability:

{
    "name": "test",
    "streets[1]": "A01,A02,A03",
    "streets[2]": "A01,A02,A03"
}

Thanks for support C:

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

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

发布评论

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

评论(1

策马西风 2025-01-22 05:32:12

我通过放置“

$app->addBodyParsingMiddleware();

我不知道它为什么起作用以及它如何真正改变”来解决。

https://stackoverflow.com/a/58827378/7336262

I solved by putting

$app->addBodyParsingMiddleware();

I don't know why it works and how it really changes.

https://stackoverflow.com/a/58827378/7336262

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