postmanpostman-pre-request-script

Convert text to Base64 automatically for sending requests over WebSocket


I would like to use Postman for testing out Websocket based APIs. Our Java application server protocol is text encoded into Base64.

for example, this is a typical request:

Z3{10153705653168884,3,10006478,{10153705653168884,first_name,,c,,xxxx@gmail.com},{10154002612579928,10154149445273051}}

If I send it like this via Postman the server cannot decode it and it doesn't work.

If I first encode it using Base64 and then send it via Postman websocket then it works perfectly!

That brings me to my question: How can I automate this process of encoding requests from text to Base64 and then send it via Postman?? I don't want for every request to use an external tool that makes the conversion and then use it in Postman.

Postman HTTP REST supports Scripts that allows you to do Pre-send data manipulations which is exactly what I need for Websockets.

Any idea?

I tried using Postman with data I converted into Base64 using external tool and it works!

But couldn't find a way to automate this process using scripting so I don't need to use any external tool.

I want everything to work from within Postman.


Solution

  • const requestData = 'Z3{10153705653168884,3,10006478,{10153705653168884,first_name,,c,,xxxx@gmail.com},{10154002612579928,10154149445273051}}';
    const encodedData = btoa(requestData);
    pm.environment.set('encodedRequestData', encodedData);
    
    {{encodedRequestData}}