Body transform problems and questions

Hi all,

I am currently in the middle of publishing an internal API and would really like to use Tyk Cloud as an authentication and mapping layer. The internal API is unfortunately not changeable and the public API shall only return a subset of the fields provided by the internal API - some fields also under a new label. So my approach is to use body transforms on the response content (JSON) provided by the internal API in order to relabel and remove certain fields.

However, I am struggling with several issues:

  1. When setting up a body transform and a URL Rewrite rule the body transformation is not executed. I am still getting the original API response.

  2. Whenever I run a body transform on any request, as shown in your docs, all REST clients (Postman, various other REST clients) will stop processing the response. They simply output errors like “ERROR: Failed to contact server” (insomnia), Could not get any response (postman), etc. It’s like the connection just broke off for these clients. cURL, however, works just fine and gets the response without complaint. I compared the output in curl before and after the change,a long with status headers and they are identical. When I remove the body transform the response is recognized by all clients again.

  3. How does body transform handle non 200 responses with errors inside? The body transform template would not fit the content any more.

  4. I am not yet very versed in go template. Is it possible to walk through nested responses change the output of each item accordingly?

Can the output of e.g.

[
  {
    "country": "US",
    "status": "Active",
    "logoHash": "7840b16aaadf5f181c8d731550a8e6fd",
    "category": "n/a",
    "logoPrint": null,
    "logoPrintHash": null
  },
  {
    "country": "DE",
    "status": "Active",
    "logo": null,
    "logoHash": null,
    "category": "n/a",
    "logoPrint": null,
    "logoPrintHash": null
  }
]

be changed to:

[
  {
    "country": "US",
    "logoHash": "7840b16aaadf5f181c8d731550a8e6fd",
  },
  {
    "country": "DE",
    "logoHash": null,
  }
]

?

Thanks a lot!!!

Hi,
I’ve tried to answer your question below:

Response transforms need to be targetted on the rewritten URL, not on the inbound one.

Not sure what’s wrong here, we test with Postman all the time, body trnsforms only impact the body of the request and change the content size param, nothing more. Maybe you are responding with the wrong headers. Or the template is crashing, also likely.

The body transform will just try to transform the returned body, regardless of status code

Not really, but you could look at the Hugo Go Template Primer, which is really quite good.

In your example, you may have a problem, since the inbound JSON object has no anchorable fields, so you can’t identify the list as a golang template variable (you might be able to use . (the main context dot), but you’d need to experiment.

Hope that helps! :slightly_smiling:
Martin

Hi Martin,

thanks for your quick reply!!!

That puzzled me a bit, but ok. So I created a second endpoint pointing to the internal API endpoint, setup the body transformation there and put the endpoint on the blacklist, so it won’t be accessible. Works! Awesome! Alas, I am running into the second issue again:

I find this really weird too, but perhaps I am doing something wrong?! I have attached a short screencast to illustrate (I even tried with something simpler, like removing all but on field - doing the transform on another endpoint. But the result is always the same)

http://screencast.com/t/DxdNOOImWM

I will look into Hugo!

So, as error messages look entirely different I need to be able to handle this via conditionals in the template.

@gawonmi Did you ever get a fix for your second issue - the one where Postman was returning “Could not get any response”? I’m experiencing the same thing. This is my first attempt at transforming a response body.