无法使用Postman将POST变量发送到localhost上的php脚本 [英] Can not send POST variables to php script on localhost using Postman

查看:1351
本文介绍了无法使用Postman将POST变量发送到localhost上的php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在iOS / Swift中开发Web应用程序的客户端,现在我正在测试与服务器通信的部分。我在localhost上设置了一个基本网站:

I am developing the client side of a web application in iOS/Swift, and right now I am testing the part that communicates with the server. I setup a basic website on localhost at:

http:// localhost / ~username / ConnectivityTest / login

(对应于Mac文件系统上的 / Users / username / Sites / ConnectivityTest / login )。

(which corresponds to /Users/username/Sites/ConnectivityTest/login on my Mac's file system).

上面目录中的服务器端脚本( index.php )是:

The server side script (index.php on the directory above) is:

<?PHP
    $userId   = $_POST["userId"];
    $password = $_POST["password"];

    if (empty($userId) || empty($password)){

        echo "Error: empty post variables";
    }
    else{
        // Process credentials...

我在iOS上使用 NSURLSession API,但我注意到无论我如何配置我的请求,即使连接成功(即返回一个200的http代码和响应主体为数据), POST 变量在服务器端不可用(空)。

I am using the NSURLSession API on iOS, but I noticed that no matter how I configure my requests, even though the connection succeeds (i.e., returns an http code of 200 and the response body as data), the POST variables are unavailable (empty) on the server side.

所以我决定尝试在浏览器上使用Postman手动发送请求(试图排除我的iOS / Swift代码上的任何错误),但我不这样做知道我应该如何配置它(我不熟悉HTTP,这对我来说仍然有点混乱):

So I decided to try sending the request manually using Postman on the browser (to try to rule out any mistakes on my iOS/Swift code), but I don't know how I should configure it (I am not versed in HTTP, it all is still a bit confusing to me):


  1. 我应该将 Content-Type 标头设置为 application / json application / x-www -form-urlencoded ,或什么?

  1. Should I set the Content-Type header to application/json, application/x-www-form-urlencoded, or what?

我应该将身体数据发送为 form-data x-www-form-urlencoded raw

Should I send the body data as form-data, x-www-form-urlencoded or raw?

在Postman中,我按如下方式设置正文数据(原始数据):

In Postman, I set the body data (raw) as follows:

{
    "userId":"my-user-name",
    "password":"123456"
}

Alternativley, form-data ,它是:

Alternativley, as form-data, it is:

userId     my-user-name      [Text]
password   12345             [Text]

作为 x-www-form-urlen编码,它是:

userId     my-user-name      
password   12345        

我尝试的一切都给我回复错误:空帖变量我在代码的错误路径中设置(即 $ _ POST ['userId'] $ _ POST ['password'] 是空的。

Everything I try gives me the response "Error: empty post variables" that I set in my code's error path (i.e., $_POST['userId'] or $_POST['password'] are empty).

相反,如果我将变量作为URL参数传递:

If, instead, I pass the variables as URL parameters:

http://localhost/~username/ConnectivityTest/login/index.php?userId=my-user-name&password=12345

...并在脚本中以& _GET ['userId'] 的形式访问它们, $ _ GET ['password'] ,它运作正常。

...and access them in the script as &_GET['userId'] and $_GET['password'], it works fine.

我缺少什么?

更新:我在与php文件相同的目录中创建了一个HTML文件:

UPDATE: I created an HTML file in the same directory as the php file:

<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>

如果我在浏览器中加载上述页面,请填写字段并提交表单, $ _ POST 我的php脚本上的变量获取正确的值。所以php代码是正确的,我在Postman中设置我的请求错误(仍然不知道为什么)。

If I load the above page in a browser, fill in the fields and submit the form, the $_POST variables on my php script get the correct values. So the php code is correct, and I am setting up my request wrong in Postman (still don't know why).

UPDATE 2:为了防止localhost出现问题,我将脚本移动到我控制的共享wb托管服务,但结果是一样的。

UPDATE 2: Just in case there was a problem with localhost, I moved the script to a shared wb hosting service that I control, but the result is the same.

更新3:以前我一定错过了它,但有一个设置让我工作:

UPDATE 3: I must have missed it somehow before, but there is ONE setup that I got working:

标题:内容类型应用程序/ x-www-form-urlencoded

正文(原始): userId = my-user-name& password = 123456

然而,这限制了我到关键/值的平面列表;如果我希望向服务器发送更多结构化(即嵌套)数据,我需要JSON支持...

However, this restricts me to flat lists of key/value; If I wish to send more structured (i.e., nested) data to the server I need JSON support...

推荐答案

搜索后在这里和那里,我发现只有当你以形式-ie, application / x发送它们时,帖子正文数据才会进入 $ _ POST 变量-www-form-urlencoded - (我猜这就是 $ _ POST 代表的,而不是用于请求的方法)。 如果我说的是不正确的话,请纠正我。

After searching here and there, I discovered that the post body data gets into the $_POST variables only when you send them as a form -i.e., application/x-www-form-urlencoded- (I guess that is what $_POST stands for, not the method used for the request). Correct me if I'm saying something that isn't correct.

使用Content-Type of 应用程序时/ json ,如下代码可以解决问题:

When using a Content-Type of application/json, code like the following does the trick:

$data = json_decode(file_get_contents('php://input'), true);

$userId = $data["userId"];
$password = $data["password"];

我想这是非常基本的东西,但话说回来,我对HTTP的了解非常有限。 。

I guess this is very basic stuff, but then again, my knowledge of HTTP is very limited...

这篇关于无法使用Postman将POST变量发送到localhost上的php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆