# Pre-response Script - decode response before displaying it in Body window

**URL:** <https://community.postman.com/t/pre-response-script-decode-response-before-displaying-it-in-body-window/4015>\
**Category:** Help Hub\
**Tags:** encryption\
**Created:** [February 4, 2019, 9:20am UTC](https://community.postman.com/t/pre-response-script-decode-response-before-displaying-it-in-body-window/4015 "2019-02-04T09:20:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Misiu](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/misiu/32/478_2.png) [@Misiu](https://community.postman.com/u/Misiu)\
**Post date:** [February 4, 2019, 9:20am UTC](https://community.postman.com/t/pre-response-script-decode-response-before-displaying-it-in-body-window/4015/1 "2019-02-04T09:20:18Z")

</div>

I’m trying to move my entire workflow into Postman.  
Endpoint I’m trying to use is returning encrypted responses for anything that is returned with status code 200.  
I have a C# app that I use to test this endpoint (to see decoded response) but I want to use Postman.

Currently, there is a pre-request script, but is there a pre-response script? I would like to check response status code and if it is OK (200) I would like to decode response.

Inside C# I use this code to encrypt/decrypt response in my test app:

```auto
public static string EncryptRijndael(string input, string key)
{
    var aes = new RijndaelManaged
    {
        KeySize = 256,
        BlockSize = 256,
        Padding = PaddingMode.PKCS7,
        Mode = CipherMode.CBC,
        Key = Encoding.UTF8.GetBytes(key)
    };

    aes.GenerateIV();

    var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

    byte[] buffer;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
        {
            byte[] bytes = Encoding.UTF8.GetBytes(input);
            cs.Write(bytes, 0, bytes.Length);
        }
        buffer = ms.ToArray();
    }

    buffer = buffer.Concat(aes.IV).ToArray();

    aes.Dispose();
    return Convert.ToBase64String(buffer);
}

public static string DecryptRijndael(string input, string key)
{
    var aes = new RijndaelManaged
    {
        KeySize = 256,
        BlockSize = 256,
        Mode = CipherMode.CBC,
        Padding = PaddingMode.PKCS7,
        Key = Encoding.UTF8.GetBytes(key)
    };

    byte[] xXml = Convert.FromBase64String(input);
    var buffer = xXml.Take(xXml.Length - aes.IV.Length).ToArray();
    var iv = xXml.Skip(xXml.Length - aes.IV.Length).ToArray();

    aes.IV = iv;
    var decrypt = aes.CreateDecryptor();
    byte[] xBuff;
    using (var ms = new MemoryStream())
    {
        using (var cs = new CryptoStream(ms, decrypt, CryptoStreamMode.Write))
        {
            cs.Write(buffer, 0, buffer.Length);
        }
        xBuff = ms.ToArray();
    }
    aes.Dispose();
    String output = Encoding.UTF8.GetString(xBuff);
    return output;
}

```

---

<div class="post-metadata">

**Author:** ![usuario\_00001](https://avatars.discourse-cdn.com/v4/letter/u/da6949/32.png) [@usuario\_00001](https://community.postman.com/u/usuario_00001)\
**Post date:** [April 23, 2021, 12:12am UTC](https://community.postman.com/t/pre-response-script-decode-response-before-displaying-it-in-body-window/4015/2 "2021-04-23T00:12:15Z")

</div>

Hello  
did you achieve your goal?  
if is YES please share =)

---

<div class="post-metadata">

**Author:** ![spamvictim](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@spamvictim](https://community.postman.com/u/spamvictim)\
**Post date:** [January 14, 2022, 6:17pm UTC](https://community.postman.com/t/pre-response-script-decode-response-before-displaying-it-in-body-window/4015/3 "2022-01-14T18:17:36Z")

</div>

You can’t change the response body, but if you wanna avoid console.log’ing from a test you can simply use pm.visualizer.set(…) to write something into the visualize tab.  
There you can write html code which gets rendered, so as long your response is formatted to html you can print it in a formatted form similiar to the raw tab.  
Assuming your decrypted response body, which is in json, is inside the decData variable your code could be as follow:

```javascript
const decData = '{"a": "foo", "b": [1,2,3,"bar"]}';
const dataParsed = JSON.parse(decData);
console.log(dataParsed);
const formattedDataHtmlFormat = JSON.stringify(dataParsed, null, 2).replace(/ /g, "&nbsp;").replace(/\n/g, "<br />");
pm.visualizer.set(formattedDataHtmlFormat);

```

Going from here you could also do some formatting to get some colored html output for code highlighting, but for being able to see it in a formatted structure was good enough.
