Welcome Guest, Not a member yet? Register   Sign In
Headers not properly set with response->setContentType
#1

(This post was last modified: 09-28-2023, 07:40 AM by bgeneto.)

Any ideas why setting headers like this doesn't work:

Code:
$this->response->setContentType('text/event-stream') 

but using like this works?

Code:
header('Content-Type: text/event-stream')

I've build the following MWE running on a fresh install of CI4's appstarter: 

PHP Code:
<?php

namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
Headers extends Controller
{
    public function not_working(): void
    
{
        $this->response->setContentType('text/event-stream');
        $this->response->setHeader('Content-Type''text/event-stream')
 
    ->setHeader('Cache-Control''no-cache')
 ->
setHeader('Connection''keep-alive')
 ->
setHeader('Access-Control-Allow-Origin''*');

 
// data to be sent
 
$data = [
 
'timestamp' => time()
 ];

 
// Send the SSE data to the client
 
echo "data: " json_encode($data) . "\n\n";

 @
flush();
    }

    public function working(): void
    
{
 
header('Content-Type: text/event-stream');
 
header('Cache-Control: no-cache');
 
header('Connection: keep-alive');
 
header('Access-Control-Allow-Origin: *');

 
// data to be sent
 
$data = [
 
'timestamp' => time()
 ];

 
// Send the SSE data to the client
 
echo "data: " json_encode($data) . "\n\n";

 @
flush();
    }

Routes are configured like this:

PHP Code:
<?php

use CodeIgniter\Router\RouteCollection;

/**
 * @var RouteCollection $routes
 */
$routes->get('/''Home::index');
$routes->get('/working''Headers::working');
$routes->get('/not-working''Headers::not_working'); 
You will notice that the response header of "/not-working" is "text/html; charset=UTF-8" instead of "text/stream-event", tested with Apache/2.4.56 (Win 11 and Debian 12) with or without nginx reverse proxy.
To my surprise, with LiteSpeed or OpenLiteSpeed server "/not-working" returns the correct headers  Huh
Any ideas why both "$this->response->setContentType('text/event-stream');" and "$this->response->setHeader('Content-Type', 'text/event-stream')?" does not in my controller?

TIA.
Reply
#2

Not send json raw. Send your response as "return $this->response->setJson(...)"

You not applied response in controller, this not work.
Simple CI 4 project for beginners codeigniter-expenses
Reply
#3

(09-28-2023, 09:37 AM)ozornick Wrote: Not send json raw. Send your response as "return $this->response->setJson(...)"

You not applied response in controller, this not work.

Unfortunately using "setJSON()" as suggested returns "application/json" headers instead of  "text/event-stream". That's because it calls "formatBody()".
I'm trying to implement some Server-Sent Events (SSE), so it requires "text/event-stream" headers for receiving plain text data that ends with "\n\n".
I figure out how to make it works with all tested servers (Apache, nginx, LiteSpeed). Thanks for the tip.

PHP Code:
public function working()
{
    // Set the appropriate headers for SSE
    $this->response->setContentType('text/event-stream');
    $this->response->setHeader('Cache-Control''no-cache')
        ->setHeader('Connection''keep-alive')
        ->setHeader('Access-Control-Allow-Origin'base_url())
        ->setHeader('Access-Control-Allow-Methods''GET');

    // data to be sent
    $data = [
        'timestamp' => time()
    ];
    $sse_data "retry: 5000\ndata: " json_encode($data) . "\n\n";

    // Send the SSE data to the client
    $this->response->appendBody($sse_data);
    $this->response->send();

Reply




Theme © iAndrew 2016 - Forum software by © MyBB