# Wrong Content Typ from controller

**URL:** <https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264>\
**Category:** Questions\
**Created:** [May 17, 2016, 9:39am UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264 "2016-05-17T09:39:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brillede](https://avatars.discourse-cdn.com/v4/letter/b/c37758/32.png) [@brillede](https://discourse.slimframework.com/u/brillede)\
**Post date:** [May 17, 2016, 9:39am UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264/1 "2016-05-17T09:39:08Z")

</div>

Hello everybody,  
I always get the content Typ text/html From Rest Client Tool  
I try to create a controller for my API thanks for help  
index.php

> ```
> $app = new \Slim\App(['settings' => ['displayErrorDetails' => true]]);
> $container = $app->getContainer();
> $container[\src\Controller\Opticancore::class] = function ($container) {
> return new \src\Controller\Opticancore($container);
> };
> 
> ```

> ```
> $app->get('/','\src\Controller\Opticancore:test');
>     
> $app->run();
> 
> ```

Opticancore.php

> ```
> use \Psr\Http\Message\ServerRequestInterface as Request;
> use \Psr\Http\Message\ResponseInterface as Response;
> 
> ```

> ```
> class Opticancore {
> protected $ci;
> 
> ```

> ```
> public function __construct($c) {
> $this->ci=$c;
> }
> 
> ```

> ```
> public function test(Request $request, Response $response){
> 
> ```

> ```
> $response->getBody()->write(json_encode(['YOUR_ARRAY']));
> $response = $response->withHeader(
> 'Content-type',
> 'application/json; charset=utf-8'
> );
> return $response;
> }
> 
> ```

Restclient Response  
text/html; charset=UTF-8

Why can I not set the content typ to application/json; charset=utf-8’ in the controller for the Response?

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [May 17, 2016, 8:22pm UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264/2 "2016-05-17T20:22:46Z")

</div>

Setting the response seems to work fine using your example:

```
$ curl localhost:8000 -v
* Rebuilt URL to: localhost:8000/
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.47.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Host: localhost:8000
< Connection: close
< X-Powered-By: PHP/7.0.4-7ubuntu2
< Content-type: application/json; charset=utf-8
< Content-Length: 14
< 
* Closing connection 0
["YOUR_ARRAY"]

```

What headers and response body are you seeing?

---

<div class="post-metadata">

**Author:** ![brillede](https://avatars.discourse-cdn.com/v4/letter/b/c37758/32.png) [@brillede](https://discourse.slimframework.com/u/brillede)\
**Post date:** [May 18, 2016, 5:45am UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264/3 "2016-05-18T05:45:12Z")

</div>

I use a rest client Tool  
it shows me following predefined  
 ![](https://canada1.discourse-cdn.com/flex030/uploads/slimframework/original/1X/bf3d410d6ce53577b5a198c1e65e0fe90be5190f.png)

I have test with other client tool but it shows the same

---

<div class="post-metadata">

**Author:** ![llvdl](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/llvdl/32/78_2.png) [@llvdl](https://discourse.slimframework.com/u/llvdl)\
**Post date:** [May 18, 2016, 10:09am UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264/4 "2016-05-18T10:09:45Z")

</div>

It looks like you have empty lines as part of your response. Because of these lines, the PHP interpreter may conclude that outputting has started and will ignore any new headers.

Check your source code if you have empty lines before the “\<?php" tag. Also omit the "?\>” closing tag to avoid unintented output of empty lines.

---

<div class="post-metadata">

**Author:** ![brillede](https://avatars.discourse-cdn.com/v4/letter/b/c37758/32.png) [@brillede](https://discourse.slimframework.com/u/brillede)\
**Post date:** [May 19, 2016, 9:13am UTC](https://discourse.slimframework.com/t/wrong-content-typ-from-controller/264/5 "2016-05-19T09:13:34Z")

</div>

Yes right, after this closing tag i had empty lines. Thx for help llvdl.
