PHP + PSR7:getParsedBody()始终将json上的空字符串转换为null

时间:2018-06-20 21:52:31

标签: php zend-framework psr-7

我正在将zendframework/zend-diactoros与Laravel结合使用。我在发送json数据时遇到问题

{
    "data": {
        "value": ""
    }
}

然后,在控制器中

// @var $request Psr\Http\Message\ServerRequestInterface
var_dump($request->getParsedBody())
/*
array:1 [
  "data" => array:1 [
      "value" => null // I expect "value" => ""
  ]
]

更多示例:

我发送了

{
    "data": {
        "value": null
    }
}

var_dump

var_dump($request->getParsedBody())
/*
array:1 [
  "data" => array:1 [
      "value" => null // that's OK
  ]
]

我发送了

{
    "data": {
        "value": "x"
    }
}

var_dump

var_dump($request->getParsedBody())
/*
array:1 [
  "data" => array:1 [
      "value" => "x"  // that's OK
  ]
]

只有第一个示例有问题,但是我不知道这是否正确。也许存在一种使用此PSR7库转储接收到的确切数据的方法。

0 个答案:

没有答案