Laravel 不读取 PUT 请求的 HTTP 请求负载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25387166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel doesn't read HTTP request payload for PUT request
提问by Ronni Egeriis Persson
So I am stumbling a bit here, as I have figured out that PHP will not read the HTTP request body from a PUT request. And when the Content-Type
header in the request is set to application/json
, there doesn't seem to be any way to get the body.
所以我在这里有点磕磕绊绊,因为我发现 PHP 不会从 PUT 请求中读取 HTTP 请求正文。当Content-Type
请求中的标头设置为 时application/json
,似乎没有任何方法可以获取正文。
I am using Laravel, which builds their request layer on top of Symfony2's HttpFoundation lib.
我正在使用 Laravel,它在 Symfony2 的 HttpFoundation 库之上构建了他们的请求层。
I have debugged this a bit with jQuery, and these are some example requests:
我已经用 jQuery 对此进行了一些调试,这些是一些示例请求:
Doing a request like this, I can find the content through Input::getContent()
做这样的请求,我可以通过以下方式找到内容 Input::getContent()
$.ajax({
url: 'http://api.host/profiles/12?access_token=abcdef',
type: 'PUT',
data: {"profiles":[{"name":"yolanda ellis","email":"[email protected]"}]}
});
I cannot get the content with file_get_contents('php://input')
though. jQuery per default sends the data as application/x-www-form-urlencoded
.
我无法获得内容file_get_contents('php://input')
。默认情况下,jQuery 将数据作为application/x-www-form-urlencoded
.
It becomes even more mindboggeling when I pass another Content-Type in the request. Just like Ember-Data does:
当我在请求中传递另一个 Content-Type 时,它变得更加令人难以置信。就像 Ember-Data 所做的那样:
$.ajax({
url: 'http://api.host/profiles/12?access_token=abcdef',
type: 'PUT',
data: {"profiles":[{"name":"yolanda ellis","email":"[email protected]"}]},
contentType: 'application/json'
});
The data seems nowhere to be found, when doing it like this. This means that my Ember.js app does not properly work with my API.
这样做时,数据似乎无处可寻。这意味着我的 Ember.js 应用程序无法正常使用我的 API。
What on earth is going on here?
这到底是怎么回事?
Edit
编辑
Here's a full request example as seen in Chrome DevTools: http://pastebin.com/ZEjDAsmJ
这是 Chrome DevTools 中的完整请求示例:http://pastebin.com/ZEjDAsmJ
I have found that this is a Laravel specific issue.
我发现这是 Laravel 特定的问题。
Edit 2: Answer found
编辑2:找到答案
It appears that there's a dependency in my project, which reads from php://input
when the Content-Type: application/json
header is sent with the request. This clears the stream—as pointed out in the link provided by @Mark_1—causing it to be empty when it reaches Laravel.
看来我的项目中有一个依赖项,它从请求头随请求一起发送php://input
时开始读取Content-Type: application/json
。这会清除流——正如@Mark_1 提供的链接中所指出的——导致它在到达 Laravel 时为空。
The dependency is bshaffer/oauth2-server-php
回答by Barryvdh
You should be able to use Input::json()
in your code to get the json decoded content.
您应该能够Input::json()
在代码中使用来获取 json 解码的内容。
I think you can only read the input stream once, so if a different package read the input stream before you, you can't access it.
我认为您只能读取一次输入流,因此如果不同的包在您之前读取了输入流,您将无法访问它。
Are you using OAuth2\Request::createFromGlobals()
to create the request to handle your token? You should pass in the existing request object from Laravel, so both have access to the content.
Did you read this? http://bshaffer.github.io/oauth2-server-php-docs/cookbook/laravel/That links to https://github.com/bshaffer/oauth2-server-httpfoundation-bridgewhich explains how to create a request object from an httpfoundation request object (which Laravel uses).
您是否使用OAuth2\Request::createFromGlobals()
创建请求来处理您的令牌?您应该从 Laravel 传入现有的请求对象,以便两者都可以访问内容。你读过这个吗?http://bshaffer.github.io/oauth2-server-php-docs/cookbook/laravel/链接到https://github.com/bshaffer/oauth2-server-httpfoundation-bridge,它解释了如何创建请求对象来自 httpfoundation 请求对象(Laravel 使用的)。
Something like this:
像这样的东西:
$bridgeRequest = \OAuth2\HttpFoundationBridge\Request::createFromRequest($request);
$server->grantAccessToken($bridgeRequest, $response);
So they both share the same content etc.
所以他们都共享相同的内容等。
回答by Mark_1
I found the following comment at http://php.net/manual/en/features.file-upload.put-method.php
我在http://php.net/manual/en/features.file-upload.put-method.php找到以下评论
PUT raw data comes in php://input, and you have to use fopen() and fread() to get the content. file_get_contents() is useless.
PUT 原始数据来自 php://input,你必须使用 fopen() 和 fread() 来获取内容。file_get_contents() 没用。
Does this help?
这有帮助吗?