zendframework / zend-diactoros

PSR-7 HTTP Message implementation
BSD 3-Clause "New" or "Revised" License
1.56k stars 152 forks source link

getParsedBody() always convert empty strings on json to null #305

Closed pablorsk closed 6 years ago

pablorsk commented 6 years ago

I'm im using zendframework/zend-diactoros with Laravel. I have a problem when I sent json data like

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

Then, in controller

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

More examples:

-> I sent

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

var_dump

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

-> I sent

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

var_dump

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

This is not a Diactoros issue; we do not do JSON parsing within this library. The likely culprit is middleware or a process earlier in your application that parses the body and injects it in the Request body parameters.