Jump to content

Twig to respond with 204 status code


NotionCommotion

Recommended Posts

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();
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.