Closed Serhioromano closed 9 years ago
Have you tried: withJson method? What you get with it?
https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Response.php#L285
Still the same.
where is this code?
@Serhioromano are you sure you are returning the correct response object, remember PSR7 response object is immutable. Try the following below:
$app->get('/', function ($request, $response) {
$data = [
['name' => 'Tester'],
['name' => 'Tester 1'],
];
return $response->withJson($data);
});
Very simple. This is my whole file as it is now.
<?php
define('_API', 1);
$loader = require '../vendor/autoload.php';
$app = new \Slim\App();
use App\Lib\Language;
use App\Lib\Util;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
$app->map(['POST'], '/{module}/{group}/{action}[/{id}]',
function (ServerRequestInterface $request, ResponseInterface $response, $params) use ($app)
{
/**
* @var $module string
* @var $group string
* @var $action string
* @var $id string
*/
extract($params);
try
{
$body = $request->getParsedBody();
if($body === NULL)
{
throw new \Exception("Cannot get Params");
}
Language::init($module, $body);
$data = Util::apiExecute($request, $response, $app, $params, TRUE, $body);
$content_type = 'text/html;charset=utf-8';
if(is_array($data) || is_object($data) || $data == '1')
{
$content_type = 'application/json;charset=utf-8';
$data = json_encode(array(
'error' => FALSE,
'result' => $data
));
}
$response->withStatus(200)
->withHeader('Content-type', $content_type)
->write($data);
}
catch(Exception $e)
{
$data = json_encode(array(
'error' => TRUE,
'code' => $e->getCode(),
'message' => 'Error: ' . $e->getMessage(),
'module' => $module,
'group' => $group,
'file' => $e->getFile(),
'line' => $e->getLine(),
//'trace' => $e->getTrace(),
'action' => $action
));
$response->withStatus(200)
->withHeader('Content-type', 'application/json;charset=utf-8')
->write($data);
}
});
$app->run();
And I did try withJson
instead of
$response->withStatus(200)
->withHeader('Content-type', $content_type)
->write($data);
By the way @silentworks if I try your example it does work fine. I do not understand what is the difference.
You need to return the $response
from your closure.
Hey!! Yes it works now. Although strange that it doe snot work like this
$response->withStatus(200)
->withHeader('Content-type', $content_type)
->write($data);
return $response;
But it works like this
return $response->withStatus(200)
->withHeader('Content-type', $content_type)
->write($data);
For the first example, you need to reassign to $response as its immutable:
$response = $response->withStatus(200) ->withHeader('Content-type', $content_type) ->write($data); return $response;
FWIW, the withJSON
helper function that was added to Slim 3 can only return application/json
which causes problems in certain versions of MSIE when it is a response after an upload (IE will just download the JSON as a file, go figure...). So being able to set the Content-Type
as an option would be a nice addition.
I have this problem too. Does not work: "return $ response-> withJson ('Erro na remoção', 400);"
It only works like this: "return $ response -> withStatus (400) -> withHeader ("Content-Type", "application / json; charset = utf-8") -> write ('Erro na remoção'); "
i solved this with this one $response->getBody()->write(json_encode($tickets)); return $response->withAddedHeader('Content-Type', 'application/json');
I do not know why. I do like this.
But in all my HTTP clients and tester I see that I have
Request
Response
It always gives
Content-Type: text/html
no matter what I do. I even tried PHPheader()
function. What could be the reason?Everything works, it is just my clients do not automatically process body as JSON and no auto format.