简体   繁体   English

无法使用Postman将POST变量发送到localhost上的php脚本

[英]Can not send POST variables to php script on localhost using Postman

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. 我正在iOS / Swift中开发Web应用程序的客户端,现在我正在测试与服务器通信的部分。 I setup a basic website on localhost at: 我在localhost上设置了一个基本网站:

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

(which corresponds to /Users/username/Sites/ConnectivityTest/login on my Mac's file system). (对应于我的Mac文件系统上的/Users/username/Sites/ConnectivityTest/login )。

The server side script ( index.php on the directory above) is: 服务器端脚本(上面目录中的index.php )是:

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

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

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

I am using the NSURLSession API on iOS, but I noticed that no matter how I configure my requests, even though the connection succeeds (ie, returns an http code of 200 and the response body as data), the POST variables are unavailable (empty) on the server side. 我在iOS上使用NSURLSession API,但我注意到无论我如何配置我的请求,即使连接成功(即返回http代码200和响应正文作为数据), POST变量也不可用(空)在服务器端。

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): 所以我决定尝试在浏览器上使用Postman手动发送请求(试图排除我的iOS / Swift代码上的任何错误),但我不知道应该如何配置它(我不熟悉HTTP,它一切都让我有点困惑):

  1. Should I set the Content-Type header to application/json , application/x-www-form-urlencoded , or what? 我应该将Content-Type标头设置为application/jsonapplication/x-www-form-urlencoded ,还是什么?

  2. Should I send the body data as form-data , x-www-form-urlencoded or raw ? 我应该将身体数据作为form-datax-www-form-urlencoded还是raw

In Postman, I set the body data (raw) as follows: 在Postman中,我将身体数据(原始)设置如下:

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

Alternativley, as form-data , it is: Alternativley,作为form-data ,它是:

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

As x-www-form-urlencoded , it is: 作为x-www-form-urlencoded ,它是:

userId     my-user-name      
password   12345        

Everything I try gives me the response "Error: empty post variables" that I set in my code's error path (ie, $_POST['userId'] or $_POST['password'] are empty). 我尝试的一切都给了我在我的代码的错误路径中设置的响应"Error: empty post variables" (即$_POST['userId']$_POST['password']为空)。

If, instead, I pass the variables as URL parameters: 相反,如果我将变量作为URL参数传递:

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

...and access them in the script as &_GET['userId'] and $_GET['password'] , it works fine. ...并在脚本中以&_GET['userId']$_GET['password']访问它们,它工作正常。

what am I missing? 我错过了什么?

UPDATE: I created an HTML file in the same directory as the php file: 更新:我在与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>

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. 如果我在浏览器中加载上面的页面,填写字段并提交表单,我的PHP脚本上的$_POST变量获取正确的值。 So the php code is correct, and I am setting up my request wrong in Postman (still don't know why). 所以php代码是正确的,我在Postman中设置我的请求错误(仍然不知道为什么)。

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. 更新2:为了防止localhost出现问题,我将脚本移动到我控制的共享wb托管服务,但结果是一样的。

UPDATE 3: I must have missed it somehow before, but there is ONE setup that I got working: 更新3:我以前肯定错过了它,但有一个设置我工作:

Headers: Content-Type application/x-www-form-urlencoded 标题: Content-Type application/x-www-form-urlencoded

Body ("raw"): userId=my-user-name&password=123456 正文(“raw”): userId=my-user-name&password=123456

However, this restricts me to flat lists of key/value; 但是,这限制了我对键/值的平面列表; If I wish to send more structured (ie, nested) data to the server I need JSON support... 如果我希望向服务器发送更多结构化(即嵌套)数据,我需要JSON支持...

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 -ie, application/x-www-form-urlencoded - (I guess that is what $_POST stands for, not the method used for the request). 在这里和那里搜索之后,我发现只有当你将它们作为一个形式-ie, application/x-www-form-urlencoded发送时,帖子正文数据才会进入$_POST变量 - (我猜这就是$_POST代表的内容) ,而不是用于请求的方法)。 Correct me if I'm saying something that isn't correct. 如果我说的是不正确的话,请纠正我。

When using a Content-Type of application/json , code like the following does the trick: 当使用Content-Type of application/json ,如下代码可以解决问题:

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

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

I guess this is very basic stuff, but then again, my knowledge of HTTP is very limited... 我想这是非常基本的东西,但话说回来,我对HTTP的了解非常有限......

A mistake I made when first using Postman was setting the params when using the POST method which would fail. 我第一次使用Postman时犯的错误是在使用失败的POST方法时设置了参数。 I tried your Update 3 which worked and then I realized there were key value pairs in the Body tab. 我尝试了你的Update 3,然后我意识到Body选项卡中有关键值对。

Removing the params, setting the Body to "x-www-form-urlencoded" and adding the variables to be posted here works as expected. 删除参数,将Body设置为“x-www-form-urlencoded”并添加要在此处发布的变量按预期工作。

My oversight was I figured there would be a single section to enter the values and the method would determine how to pass them along which makes sense in case you would like to send some parameters in the URL with the POST 我的疏忽是我认为会有一个部分输入值,并且该方法将决定如何传递它们,这是有意义的,以防您想要使用POST在URL中发送一些参数

Postman Chrome Extension  -  Post Body

Comment by @FirstOne saved me. @FirstOne的评论救了我。 I added a forward slash to the URL and it solved the problem. 我在URL中添加了正斜杠,它解决了问题。 This way, my php script could detect the request as POST even without setting header to 这样,即使没有设置标头,我的php脚本也可以将请求检测为POST

Content-Type application/x-www-form- urlencoded 内容类型应用程序/ x-www-form- urlencoded

I also tested my code without a header and it works fine. 我也没有标题测试我的代码,它工作正常。 I tested with Content-type: application/json too and it works fine. 我测试了Content-type:application / json,它运行正常。

if($_SERVER['REQUEST_METHOD] = 'POST') { echo 'Request is post'; if($ _ SERVER ['REQUEST_METHOD] ='POST'){echo'Request is post'; } }

My script returned 'Request is post' using RESTEasy - Rest client on Chrome. 我的脚本在Chrome上使用RESTEasy - Rest客户端返回“请求发布”。 Thanks, FirstOne. 谢谢,FirstOne。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM