无法使用 Postman 将 POST 变量发送到本地主机上的 php 脚本
Can not send POST variables to php script on localhost using Postman
我正在 iOS/Swift 开发 Web 应用程序的客户端,现在我正在测试与服务器通信的部分。我在本地主机上设置了一个基本网站:
http://localhost/~username/ConnectivityTest/login
(对应于我的 Mac 文件系统上的 /Users/username/Sites/ConnectivityTest/login
)。
服务器端脚本(index.php
在上面的目录中)是:
<?PHP
$userId = $_POST["userId"];
$password = $_POST["password"];
if (empty($userId) || empty($password)){
echo "Error: empty post variables";
}
else{
// Process credentials...
我在 iOS 上使用 NSURLSession
API,但我注意到无论我如何配置我的请求,即使连接成功(即 returns 200 的 http 代码和响应 body 作为数据),POST
变量在服务器端不可用(空)。
所以我决定尝试在浏览器上使用 Postman 手动发送请求(试图排除我的 iOS/Swift 代码中的任何错误),但我不知道应该如何配置它(我我不精通 HTTP,这对我来说仍然有点混乱):
我应该将 Content-Type
header 设置为 application/json
、application/x-www-form-urlencoded
还是什么?
我应该将 body 数据发送为 form-data
、x-www-form-urlencoded
还是 raw
?
在 Postman 中,我将 body 数据(原始)设置如下:
{
"userId":"my-user-name",
"password":"123456"
}
另类,如form-data
,则为:
userId my-user-name [Text]
password 12345 [Text]
作为x-www-form-urlencoded
,是:
userId my-user-name
password 12345
我尝试的所有操作都会给我在代码错误路径中设置的响应 "Error: empty post variables"
(即,$_POST['userId']
或 $_POST['password']
为空)。
如果相反,我将变量作为 URL 参数传递:
http://localhost/~username/ConnectivityTest/login/index.php?userId=my-user-name&password=12345
...并在脚本中以 &_GET['userId']
和 $_GET['password']
访问它们,它工作正常。
我错过了什么?
更新: 我在与 php 文件相同的目录中创建了一个 HTML 文件:
<html>
<body>
<form action="index.php" method="post">
User name: <input type="text" name="userId"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
如果我在浏览器中加载上述页面,填写字段并提交表单,我的 php 脚本中的 $_POST
变量会获得正确的值。所以 php 代码是正确的,我在 Postman 中设置了错误的请求(仍然不知道为什么)。
更新 2: 以防万一本地主机出现问题,我将脚本移至我控制的共享 wb 托管服务,但结果是一样的。
更新 3: 我以前一定是错过了它,但是我有一个设置可以工作:
Headers: Content-Type application/x-www-form-urlencoded
Body ("raw"): userId=my-user-name&password=123456
但是,这将我限制为 key/value 的平面列表;如果我希望向服务器发送更多结构化(即嵌套)数据,我需要 JSON 支持...
在这里和那里搜索后,我发现 post 正文数据只有在您将它们作为表单发送时才会进入 $_POST
变量 - 即 application/x-www-form-urlencoded
-(我猜想这就是 $_POST
代表的意思,而不是用于请求的方法)。 如果我说的不正确,请纠正我。
当使用 application/json
的 Content-Type 时,像下面这样的代码可以达到目的:
$data = json_decode(file_get_contents('php://input'), true);
$userId = $data["userId"];
$password = $data["password"];
我想这是非常基础的东西,但话又说回来,我对 HTTP 的了解非常有限...
我第一次使用 Postman 时犯的一个错误是在使用 POST 方法时设置了参数,这会失败。我尝试了您的 Update 3,它起作用了,然后我意识到“正文”选项卡中有键值对。
删除参数,将 Body 设置为 "x-www-form-urlencoded" 并添加要在此处发布的变量按预期工作。
我的疏忽是,我认为只有一个部分可以输入值,该方法将确定如何传递它们,如果您想在 URL 中发送一些参数,这很有意义POST
@FirstOne 的评论救了我。我在 URL 中添加了一个正斜杠,它解决了问题。这样,即使没有将 header 设置为
,我的 php 脚本也可以将请求检测为 POST
Content-Type application/x-www-form-
urlencoded
我也在没有 header 的情况下测试了我的代码,它工作正常。我也用 Content-type: application/json 测试过,效果很好。
if($_SERVER['REQUEST_METHOD] = 'POST') { echo 'Request is post'; }
我的脚本使用 RESTEasy 返回 'Request is post' - Rest client on Chrome。
谢谢,FirstOne。
我正在 iOS/Swift 开发 Web 应用程序的客户端,现在我正在测试与服务器通信的部分。我在本地主机上设置了一个基本网站:
http://localhost/~username/ConnectivityTest/login
(对应于我的 Mac 文件系统上的 /Users/username/Sites/ConnectivityTest/login
)。
服务器端脚本(index.php
在上面的目录中)是:
<?PHP
$userId = $_POST["userId"];
$password = $_POST["password"];
if (empty($userId) || empty($password)){
echo "Error: empty post variables";
}
else{
// Process credentials...
我在 iOS 上使用 NSURLSession
API,但我注意到无论我如何配置我的请求,即使连接成功(即 returns 200 的 http 代码和响应 body 作为数据),POST
变量在服务器端不可用(空)。
所以我决定尝试在浏览器上使用 Postman 手动发送请求(试图排除我的 iOS/Swift 代码中的任何错误),但我不知道应该如何配置它(我我不精通 HTTP,这对我来说仍然有点混乱):
我应该将
Content-Type
header 设置为application/json
、application/x-www-form-urlencoded
还是什么?我应该将 body 数据发送为
form-data
、x-www-form-urlencoded
还是raw
?
在 Postman 中,我将 body 数据(原始)设置如下:
{
"userId":"my-user-name",
"password":"123456"
}
另类,如form-data
,则为:
userId my-user-name [Text]
password 12345 [Text]
作为x-www-form-urlencoded
,是:
userId my-user-name
password 12345
我尝试的所有操作都会给我在代码错误路径中设置的响应 "Error: empty post variables"
(即,$_POST['userId']
或 $_POST['password']
为空)。
如果相反,我将变量作为 URL 参数传递:
http://localhost/~username/ConnectivityTest/login/index.php?userId=my-user-name&password=12345
...并在脚本中以 &_GET['userId']
和 $_GET['password']
访问它们,它工作正常。
我错过了什么?
更新: 我在与 php 文件相同的目录中创建了一个 HTML 文件:
<html>
<body>
<form action="index.php" method="post">
User name: <input type="text" name="userId"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
如果我在浏览器中加载上述页面,填写字段并提交表单,我的 php 脚本中的 $_POST
变量会获得正确的值。所以 php 代码是正确的,我在 Postman 中设置了错误的请求(仍然不知道为什么)。
更新 2: 以防万一本地主机出现问题,我将脚本移至我控制的共享 wb 托管服务,但结果是一样的。
更新 3: 我以前一定是错过了它,但是我有一个设置可以工作:
Headers: Content-Type application/x-www-form-urlencoded
Body ("raw"): userId=my-user-name&password=123456
但是,这将我限制为 key/value 的平面列表;如果我希望向服务器发送更多结构化(即嵌套)数据,我需要 JSON 支持...
在这里和那里搜索后,我发现 post 正文数据只有在您将它们作为表单发送时才会进入 $_POST
变量 - 即 application/x-www-form-urlencoded
-(我猜想这就是 $_POST
代表的意思,而不是用于请求的方法)。 如果我说的不正确,请纠正我。
当使用 application/json
的 Content-Type 时,像下面这样的代码可以达到目的:
$data = json_decode(file_get_contents('php://input'), true);
$userId = $data["userId"];
$password = $data["password"];
我想这是非常基础的东西,但话又说回来,我对 HTTP 的了解非常有限...
我第一次使用 Postman 时犯的一个错误是在使用 POST 方法时设置了参数,这会失败。我尝试了您的 Update 3,它起作用了,然后我意识到“正文”选项卡中有键值对。
删除参数,将 Body 设置为 "x-www-form-urlencoded" 并添加要在此处发布的变量按预期工作。
我的疏忽是,我认为只有一个部分可以输入值,该方法将确定如何传递它们,如果您想在 URL 中发送一些参数,这很有意义POST
@FirstOne 的评论救了我。我在 URL 中添加了一个正斜杠,它解决了问题。这样,即使没有将 header 设置为
,我的 php 脚本也可以将请求检测为 POSTContent-Type application/x-www-form- urlencoded
我也在没有 header 的情况下测试了我的代码,它工作正常。我也用 Content-type: application/json 测试过,效果很好。
if($_SERVER['REQUEST_METHOD] = 'POST') { echo 'Request is post'; }
我的脚本使用 RESTEasy 返回 'Request is post' - Rest client on Chrome。 谢谢,FirstOne。