NotionCommotion Posted February 5, 2017 Share Posted February 5, 2017 PHP should return only status code 204 if the request was successful, else status code 422 along with a description of the error if an error occurred. Firebug has been displaying error XML Parsing Error: no root element found when it receives a 204 status code. I finally tracked it down to the following. It appears that if $rs is [null,204], I am telling slim to return JSON, however, 204 should not have any content. How should the below script be modified to prevent these errors? <?php require __DIR__.'/../vendor/autoload.php'; $app = new \Slim\App(); $app->get('/{name}', function (\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response) { $rs=($request->getAttribute('name')=='error')?["error message",422]:[null,204]; return $response->withJson($rs[0],$rs[1]); }); $app->run(); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted February 5, 2017 Share Posted February 5, 2017 Don't respond with a JSON document when you want an empty body. Quote Link to comment Share on other sites More sharing options...
NotionCommotion Posted February 5, 2017 Author Share Posted February 5, 2017 Don't respond with a JSON document when you want an empty body. Can I do so when using twig? I tried the following but no change. return $rsp[1]==204?$response->withStatus($rsp[1],'whatever'):$response->withJson($rsp[0],$rsp[1]); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.