2018-05-13 22:22
浏览 160

可以通过HTTP POST发送布尔值吗?

I'm building a simple API in PHP, one of the body parameters must be a boolean (as per documentation), true or false. I'm using Postman to test, and it seems like I can't do it. Whenever I post true or false it is seeing it as strings. 0 or 1 does not work neither. What I am doing currently for this to work is something like this:

if($_POST['authenticate'] == 'true') {
            $_POST['authenticate'] = true;

But this seems not right. Please point me in the right direction as I am not even sure now if it is possible to POST a boolean.

图片转代码服务由CSDN问答提供 功能建议

我在PHP中构建一个简单的API,其中一个body参数必须是布尔值(根据文档) , true false 。 我正在使用Postman进行测试,似乎我无法做到。 每当我发布 true false 时,它都会将其视为字符串。 0或1也不起作用。 我目前正在做的工作是这样的:

  if($ _ POST ['authenticate'] =='true'){
 $ _POST ['  authenticate'] = true; 

但这似乎不对。 请指出正确的方向,因为我现在还不确定是否可以POST一个布尔值。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • drvvvuyia15070493
    drvvvuyia15070493 2018-05-13 22:30

    HTTP POST/GET variables will be converted into string associative arrays in PHP ($_POST $_GET). You will have to go through an explicit cast or re-assignment if you want to express the values as a different datatype.

    点赞 评论
  • dongpangbu4016
    dongpangbu4016 2018-05-13 22:37

    You could consider the problem implicitly - as in :

    if (isset($_POST["authenticate"]) { ... }

    In other words, don't POST authenticate if it is anyway to be false

    点赞 评论
  • drl2051
    drl2051 2018-05-13 22:39

    You can use:

    $is_authenticate = filter_var($_POST['authenticate'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);


    $is_authenticate = $_POST['authenticate']=='true';
    点赞 评论