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

查看:22
本文介绍了无法使用 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/jsonapplication/x-www-form-urlencoded、还是什么?

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

我应该将正文数据发送为 form-datax-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-urlencoded,就是:

userId     my-user-name      
password   12345        

我尝试的一切都会给我响应 "Error: empty post variables" 我在代码的错误路径中设置的(即 $_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>

如果我在浏览器中加载上述页面,填写字段并提交表单,我的 php 脚本中的 $_POST 变量将获得正确的值.所以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).

更新 2: 以防本地主机出现问题,我将脚本移至我控制的共享 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:

标题:Content-Type application/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...

推荐答案

在这里和那里搜索后,我发现只有当您将帖子正文数据作为 $_POST 变量发送时form -ie, application/x-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.

当使用 application/json 的 Content-Type 时,如下代码可以解决问题:

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天全站免登陆