# Add default value in Accept header

**URL:** <https://discuss.akka.io/t/add-default-value-in-accept-header/1062>\
**Category:** Akka HTTP\
**Created:** [May 17, 2018, 7:39am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062 "2018-05-17T07:39:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![linda](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@linda](https://discuss.akka.io/u/linda)\
**Post date:** [May 17, 2018, 7:39am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/1 "2018-05-17T07:39:53Z")

</div>

No matter what the user writes in the Accept header I always want the possibility to return an application/json.

For example if the user requests application/pdf in the Accept header I want primarily to return application/pdf and if the application can not return pdf, return an application/json instead.

Is this possible?

---

<div class="post-metadata">

**Author:** ![raboof](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/raboof/32/2913_2.png) [@raboof](https://discuss.akka.io/u/raboof)\
**Post date:** [May 17, 2018, 9:32am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/2 "2018-05-17T09:32:58Z")

</div>

Hello Linda,

You are correct that the Akka HTTP marshalling infrastructure will select a response object serializer taking into account the `Accept` headers on the request.

You can circumvent this negotiation by constructing the `HttpResponse` yourself. For example in Scala you can :

```auto
complete(
  HttpResponse(
    StatusCodes.OK,
    entity = HttpEntity(ContentTypes.`application/json`, """{ "foo": "bar" }""")
  )
)

```

This will respond with an `application/json` entity regardless of the `Accept` headers on the request. Is that what you were looking for?

You can find some more examples of how to construct a `HttpResponse` at [https://doc.akka.io/docs/akka-http/current/common/http-model.html#httpresponse](https://doc.akka.io/docs/akka-http/current/common/http-model.html#httpresponse) and in the API docs.

---

<div class="post-metadata">

**Author:** ![linda](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@linda](https://discuss.akka.io/u/linda)\
**Post date:** [May 17, 2018, 10:02am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/3 "2018-05-17T10:02:27Z")

</div>

Hi,  
No, it does not help.

My problem is that when the user does a post. They will then get a json back with the information about the item they have created.  
If the user has written an “incorrect” media type in Accept they will never get the information about that the item that successfully has been created. Instead they just get a 406 back with the message “Resource representation is only available with these types:  
application/json”

---

<div class="post-metadata">

**Author:** ![raboof](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/raboof/32/2913_2.png) [@raboof](https://discuss.akka.io/u/raboof)\
**Post date:** [May 18, 2018, 7:53am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/4 "2018-05-18T07:53:31Z")

</div>

> [@linda](#):
>
> they just get a 406 back with the message “Resource representation is only available with these types: application/json”

Hmm, that is interesting, I would not expect that when completing the request with an `HttpResponse` as I suggested above. Can you share the code you use to produce this response, or (even better) boil it down to a small example project showing the problem?

---

<div class="post-metadata">

**Author:** ![linda](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@linda](https://discuss.akka.io/u/linda)\
**Post date:** [May 18, 2018, 8:26am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/5 "2018-05-18T08:26:48Z")

</div>

I can not use what you suggested. Because that would cause a lot special treatment and strange code.

Instead I use headerValueByName(“Accept”) to get the requested media types. For post and delete I then check that it includes _/_ or application/json. If it does not I return 406 before I save or delete anything. When I do it this way nothing will be executed that the user will not know about.

---

<div class="post-metadata">

**Author:** ![raboof](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.akka.io/raboof/32/2913_2.png) [@raboof](https://discuss.akka.io/u/raboof)\
**Post date:** [May 18, 2018, 9:15am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/6 "2018-05-18T09:15:38Z")

</div>

> [@linda](#):
>
> I can not use what you suggested. Because that would cause a lot special treatment and strange code.

OK, I can’t speak to that of course.

> [@linda](#):
>
> Instead I use headerValueByName(“Accept”) to get the requested media types. For post and delete I then check that it includes _/_ or application/json. If it does not I return 406 before I save or delete anything.

That indeed seems like a reasonable solution, especially when we’re talking about successful responses.

---

<div class="post-metadata">

**Author:** ![linda](https://avatars.discourse-cdn.com/v4/letter/l/67e7ee/32.png) [@linda](https://discuss.akka.io/u/linda)\
**Post date:** [May 18, 2018, 9:17am UTC](https://discuss.akka.io/t/add-default-value-in-accept-header/1062/7 "2018-05-18T09:17:22Z")

</div>

Yes, I did what you suggested for error messages.
