# Error handler should return JSON

**URL:** <https://discourse.slimframework.com/t/error-handler-should-return-json/109>\
**Category:** Questions\
**Created:** [April 12, 2016, 7:59am UTC](https://discourse.slimframework.com/t/error-handler-should-return-json/109 "2016-04-12T07:59:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Chrille](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Chrille](https://discourse.slimframework.com/u/Chrille)\
**Post date:** [April 12, 2016, 7:59am UTC](https://discourse.slimframework.com/t/error-handler-should-return-json/109/1 "2016-04-12T07:59:37Z")

</div>

I’m building an API with Slim 3. How do I setup that errors should be returned as JSON? For example, 405 method not allowed is returning a HTML page by default.

---

<div class="post-metadata">

**Author:** ![akrabat](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.slimframework.com/akrabat/32/9_2.png) [@akrabat](https://discourse.slimframework.com/u/akrabat)\
**Post date:** [April 12, 2016, 8:13am UTC](https://discourse.slimframework.com/t/error-handler-should-return-json/109/2 "2016-04-12T08:13:24Z")

</div>

Slim’s error handlers will return the JSON if the client’s `Accept` header is correct.

i.e.

```
curl -H "Accept: application/json" http://myslimapp.example.com/not-an-end-point

```

will produce a response of:

```
{"message":"Not found"}
```

---

<div class="post-metadata">

**Author:** ![Chrille](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Chrille](https://discourse.slimframework.com/u/Chrille)\
**Post date:** [April 12, 2016, 8:24am UTC](https://discourse.slimframework.com/t/error-handler-should-return-json/109/3 "2016-04-12T08:24:11Z")

</div>

Cool, that worked, thanks!  
Is it possible to set the default content type to json? I will only support json in the API.

---

<div class="post-metadata">

**Author:** ![JupiterN](https://avatars.discourse-cdn.com/v4/letter/j/ac91a4/32.png) [@JupiterN](https://discourse.slimframework.com/u/JupiterN)\
**Post date:** [October 24, 2017, 3:58pm UTC](https://discourse.slimframework.com/t/error-handler-should-return-json/109/4 "2017-10-24T15:58:29Z")

</div>

Hi akrabat,  
I am using slim to write a api and run into this thing today. My app has api routes and html routes and i wrote a middleware to apply to the api routes that changes the content-type to application/json.  
If i have previously (before a exception is thrown) changed content type to “application/json” shouldn´t respond as json also?

Thanks.
