Response hook response not modified

Hi,

I have created a python plugin that tries to modify the response (under the responsehook). All goes well, and printing the new response.body contains the needed modificaitons. But when getting the response on the client (Postman), the modificaitons are not reflected. Any idea what is wrong? See part of the code below. Is is right to modify the response.body directly? Or should I do something else?

# finalResponse contains the new response body
response.body = json.dumps(finalResponse)
tyk.log("ResponseHook2 final final response: {0}".format(response.body), "info")
return response
1 Like

Problem solved, it seems that I have to replace the rawbody (as binary) instead of the body itself

Hi Juan
Thanks for letting the Community know that you solved it :metal:

@Juan_Bernabeu Iā€™m also trying to overwrite the raw-body but not working. Possible to share the code on how u you did it? After I overwrite raw_body the webservice is returning an empty body.

Python Plugin Code

from tyk.decorators import *
from gateway import TykGateway as tyk
from time import time
import json
from fieldMasking import FieldMasking

@Hook
def PIIMasking(request, response, session, metadata, spec):
    tyk.log("ResponseHook is called", "info")
    # In this hook we have access to the response object, to inspect it, uncomment the following line:
    print(response)
    tyk.log("PIIMasking: upstream returned body {0}".format(response), "info")
    tyk.log("ResponseHook: upstream returned {0}".format(response.status_code), "info")
    response.headers["injectedkey"] = "injectedvalue"

    tyk.log("PIIMasking: Printing response...{0} ".format(dir(response)), "info")
    tyk.log("PIIMasking: body type {0}.".format(type(response.body)),"info")
    tyk.log("PIIMasking: body {0}.".format(response.body),"info")
    tyk.log("PIIMasking: raw body type {0}".format(type(response.raw_body)),"info")
    tyk.log("PIIMasking: raw body {0}.".format(response.raw_body),"info")

    json_body = json.loads(response.body)
    tyk.log("PIIMasking: json_body type {0}".format(type(json_body)),"info")
    tyk.log("PIIMasking: json_body value {0}".format(json_body),"info")

    fieldMasking = FieldMasking()

    addressLine1FieldStr = "addressLine1"

    for key in json_body:
      tyk.log("Key is: {0}".format(key),"info")
      if key == addressLine1FieldStr:
         json_body[key] = fieldMasking.maskAddress(json_body[key])
         tyk.log("PIIMasking: json_body={0}".format(json_body[key]),"info")

    #response.body = json.dumps(json_body, indent=2)
    response.raw_body = json.dumps(json_body, indent=2).encode('utf-8')

    tyk.log("PIIMasking: response.body- {0}.".format(response.body),"info")
    tyk.log("PIIMasking: response.rawbody- {0}.".format(response.raw_body),"info")
    return response

@kamikazane thatā€™s weird I use the same line to write the response body

response.raw_body = json.dumps(records).encode(ā€˜UTF-8ā€™)

Where ā€˜recordsā€™ is an array with the previous response

@Juan_Bernabeu thanks. After some testing this is what I found.

When triggering the API through Postman Iā€™m seeing the modified response and this means the code is working. But when I trigger it through a browser I used to get immediate response with the JSON payload displaying immediately but now what Iā€™m getting is ā€œSafari canā€™t open the page ā€œlocalhost:8080/api/get5piidataā€ because the server unexpectedly drop the connection.ā€ I tried other browser and Iā€™m not getting anything back.

Interestingly, it is working for Postman but not the browser, do you have have the same problem? Any idea? thanks.

@kamikazane , I havenā€™t tested it with browser, due to authentication. I have only used Postman. Unfortunatelly I donā€™t have a working environment now to check it

@Juan_Bernabeu thank you so much for your help. Letā€™s see if there are anyone in the forum able to enlighten me. I would need to use the browser to call the API hence Postman alone wonā€™t cut it for me.

To provide an update. The browser issue had been resolved thanks for @Pete help with his sample code.

The issue faced by browser and not by curl/Postman is due to the content len. After modification of the raw_body there is a need to update the content length.

response.headers["Content-Length"] = str(len(response.raw_body))

Hope this help.