How to add sound for the Postman app for macOS users

First of all, maybe the reason I’m doing this is because the company refused to implement such a feature inside Postman while spending their time on doing bizarre functions. What I need is a chime every time the tests are passing (status code is from group 2XX) after the request is sent. So first of all, create a Python file, name it sound.py, and paste the text below.

import os
import subprocess
from http.server import BaseHTTPRequestHandler, HTTPServer

class SoundServer(BaseHTTPRequestHandler):
    def do_GET(self):
        # Odpowiedź dla Postmana, żeby nie wisiał w nieskończoność
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"OK")

        # Ścieżki do wbudowanych dźwięków systemowych macOS
        if "error" in self.path:
            # Niski buzzer (Basso)
            sound_path = "/System/Library/Sounds/Basso.aiff"
        else:
            # Przyjemny chime (Glass)
            sound_path = "/System/Library/Sounds/Glass.aiff"

        # Popen odpala dźwięk w tle, dzięki czemu serwer nie blokuje się na czas jego trwania
        subprocess.Popen(["afplay", sound_path])

if __name__ == "__main__":
    print("🚀 Serwer dźwiękowy Postmana działa na http://localhost:2137")
    print("Naciśnij Ctrl+C w Terminalu, aby go wyłączyć.")
    try:
        HTTPServer(("localhost", 2137), SoundServer).serve_forever()
    except KeyboardInterrupt:
        print("\nZamykanie serwera.")

Run the file python3 sound.py this will set a server for sound notifications.

Then in Postman create a global variable postman_odtworz_dzwiek and set this value to true. Do not share the value with people that don’t have a script and the rest of the code! Then add the below code to your folder in the post-scripts section.

if (pm.globals.get("postman_odtworz_dzwiek") === "true") {
const is2xxSuccess = pm.response.code >= 200 && pm.response.code < 300;

const endpoint = is2xxSuccess ? "success" : "error";

try {
pm.sendRequest(`http://localhost:2137/${endpoint}`);
} catch (e) {
console.log("Serwer dźwiękowy nie odpowiada. Upewnij się, że skrypt jest uruchomiony.");
}
}

Send a couple of requests. Of course you can change which folders sound will be present in by moving the code.

Leave a Reply

Your email address will not be published. Required fields are marked *