# Modify API response based on API request before passing it to ember data

**URL:** https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998
**Category:** Ember Data
**Created:** [April 3, 2023, 8:31pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998 "2023-04-03T20:31:00Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![niltz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/niltz/32/16259_2.png) [@niltz](https://discuss.emberjs.com/u/niltz)
#### Post date: [April 3, 2023, 8:31pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/1 "2023-04-03T20:31:00Z")

</div>

Hello folks. I need to modify api response data based on the api request input parameters. How do I do this? The serializer doesn’t seem to have access to the original request data.

For instance, say I have an api endpoint like `https://pet.api/pets/1` that returns the details for a pet. Part of the details is an optional include called `vet_visits` which is a list of past visits to the vet. If I include `&include=vet_visits` then the api will populate this list. If I don’t, then the api will return an empty list (not ideal, I would prefer that `vet_visits` is just not included in the response at all, but I can’t change this)

now, if at some point I make an api call with the `&include=vet_visits` it will populate the local ember-data store with the response. Then at some later point I make an api call without the include, it will wipe out previously loaded visits, when what I really want is to keep the previously loaded `vet_visits`. Right now, because the api returns an empty `[]` when it gets wiped out in the local store.

What I would like is to be able to tell if the include was present in the call, and if not then delete the property from the response before returning it up the chain and wiping out the previously loaded visits.

---

<div class="post-metadata">

### Author: ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)
#### Post date: [April 4, 2023, 1:38am UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/2 "2023-04-04T01:38:34Z")

</div>

Off the top of my head you could probably extract some extra information (such as request data) in the handleResponse method of the adapter, put it in the payload, and then use it as you described in the serializer. This is generally where you’d do that sort of thing if you needed information from the API response headers so I think it fits well enough.

---

<div class="post-metadata">

### Author: ![niltz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/niltz/32/16259_2.png) [@niltz](https://discuss.emberjs.com/u/niltz)
#### Post date: [April 4, 2023, 5:00pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/3 "2023-04-04T17:00:33Z")

</div>

Thanks @dknutsen. I’m looking at the docs for the Adapter, and I don’t see any “handleResponse” method:

> **[Adapter - 4.11 - Ember API Documentation](https://api.emberjs.com/ember-data/4.11/classes/Adapter)**
>
> An adapter is an object that receives requests from a store and translates them into the appropriate action to take against your persistence layer. The persistence layer is usually an HTTP API but may be anything, such as the browser's local storage....

Is this a private method that is discouraged to use?

UPDATE: never mind, I see it in the RESTAdapter documentation

> **[RESTAdapter - 4.11 - Ember API Documentation](https://api.emberjs.com/ember-data/4.11/classes/RESTAdapter/methods/handleResponse?anchor=handleResponse)**
>
> The REST adapter allows your store to communicate with an HTTP server by transmitting JSON via XHR. Most Ember.js apps that consume a JSON API should use the REST adapter. This adapter is designed around the idea that the JSON exchanged with the...

---

<div class="post-metadata">

### Author: ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)
#### Post date: [April 4, 2023, 6:53pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/4 "2023-04-04T18:53:22Z")

</div>

Ah yeah it can be a little tricky figuring out which class defines them. I usually start with the JSONAPIAdapter docs since that is the “leaf node” in the inheritance hierarchy, if you will, and work backwards from there.

---

<div class="post-metadata">

### Author: ![niltz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/niltz/32/16259_2.png) [@niltz](https://discuss.emberjs.com/u/niltz)
#### Post date: [April 5, 2023, 8:15pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/5 "2023-04-05T20:15:06Z")

</div>

So we tried this method, unfortunately the `requestData` available to us in `handleResponse` only contains the `url:string` and `method:string`. It doesn’t include the `hash:object` which includes the data for the includes.

---

<div class="post-metadata">

### Author: ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)
#### Post date: [April 10, 2023, 2:57pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/6 "2023-04-10T14:57:49Z")

</div>

Usually `includes` is serialized as a query parameter no? will require a bit of extra parsing but the options hash given to the store method isn’t preserved as far as i know.

---

<div class="post-metadata">

### Author: ![niltz](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/niltz/32/16259_2.png) [@niltz](https://discuss.emberjs.com/u/niltz)
#### Post date: [April 11, 2023, 4:54pm UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/7 "2023-04-11T16:54:43Z")

</div>

Yeah, we do some crazy stuff, like overriding the `ajax` method. I think we figured it out though. Thanks for your help, it was really useful!

---

<div class="post-metadata">

### Author: ![runspired](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/runspired/32/18654_2.png) [@runspired](https://discuss.emberjs.com/u/runspired)
#### Post date: [July 9, 2023, 5:33am UTC](https://discuss.emberjs.com/t/modify-api-response-based-on-api-request-before-passing-it-to-ember-data/19998/8 "2023-07-09T05:33:50Z")

</div>

this is in fact one of the two main reasons adapters and serializers are on the way out for EmberData
