使用 Slim 3 获取 POST PUT 参数 [英] Get POST PUT parameters with Slim 3
问题描述
我正在尝试使用 Slim 3 构建完整的 REST API.使用 Slim 2 很容易.但现在我遇到了一些问题.
I'm trying to build a full REST API with Slim 3. It was quite easy with Slim 2. But now I've got some issues.
POST 和 PUT 路由在预期中不起作用.我无法获取参数.我在文档中找到了 $request->getHeaders()
,它可以工作,但是我没有得到例如参数 length
,而是得到 HTTP_LENGHT
并且每个参数都有这个 HTTP_ 前缀.
The POST and PUT route does not work has expected. I can't get the parameters.
I found the $request->getHeaders()
on the documentations, which works but instead of getting for exemple the parameter length
, I get HTTP_LENGHT
and every parameters has this HTTP_ prefixe.
我发现了这个问题$request->getParsedBody()
为我返回一个空数组.
I found this question but $request->getParsedBody()
return an empty array for me.
我正在使用 高级 REST 客户端 这是我作为 POST 请求发送的示例:
I'm testing my API with Advanced REST Client and this is an example of what I send as a POST request:
POST /test/barrier HTTP/1.1
HOST: mydomain.com
start_lng: 2.6423183977058
start_lat: 56.865296679535
type: Something
comment: testcomment
due_date: null
content-type: application/x-www-form-urlencoded
我使用的是 Slim 3.3.0.
I'm using Slim 3.3.0.
以下是我使用 $request->getHeaders()
得到的示例:
Here is an example of what I get with $request->getHeaders()
:
["HTTP_START_LAT"]=>
array(1) {
[0]=>
string(15) "56.865296679535"
}
["HTTP_START_LNG"]=>
array(1) {
[0]=>
string(15) "2.6423183977058"
}
一件奇怪的事情是,如果我尝试对 $request->hasHeader('length');
执行操作,我会得到我的参数值,这意味着名称是正确的.我真的不明白.
One strange thing is, if I try do to $request->hasHeader('length');
I get the value of my parameter, so that means, the name are correct. I really don't understand.
我只想获取参数数组,就像在 Slim 2.* 中使用的 $app->request->post()
一样.谢谢.
I just want to get the array of the parameters as $app->request->post()
used to do in Slim 2.*. Thanks.
我的.htaccess
是这样的:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
<files db.config.ini>
deny from all
</files>
推荐答案
好的.我的错误,我只是在测试应用程序中弄错了.我将所有参数写为标题形式而不是数据形式......
Ok. My mistake, I just got it wrong in the testing app. I was writing all my parameters as headers form an not as data form...
这篇关于使用 Slim 3 获取 POST PUT 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!