Skip to content

Instantly share code, notes, and snippets.

@marcomontalbano
Last active August 6, 2024 07:10
Show Gist options
  • Save marcomontalbano/58a0a074c2aec523213c460ea2f5b322 to your computer and use it in GitHub Desktop.
Save marcomontalbano/58a0a074c2aec523213c460ea2f5b322 to your computer and use it in GitHub Desktop.
Log all incoming http requests using Deno.

HTTP Request Logger

Log all incoming http requests.

This is useful when you want to test webhooks locally!

Usage

Run this deno command first:

deno run -r --allow-net https://gist.githubusercontent.com/marcomontalbano/58a0a074c2aec523213c460ea2f5b322/raw/http-request-logger.ts

Install ngrok and run the following command into another terminal tab:

ngrok http 8085

If you're using the custom domain feature from ngrok, you could run instead the following and avoid changing the webhook callback URL every time.

ngrok http --domain="webhooks.example.com" 8085

Example

curl -g -X POST \
  'http://localhost:8085' \
  -H 'Accept: application/vnd.api+json' \
  -H 'Authorization: Bearer abcd1234' \
  -H 'Content-Type: application/vnd.api+json' \
  -d '{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "Hello World!"
    }
  }
}'

import { serve } from "https://deno.land/[email protected]/http/server.ts";
import { parse } from "https://deno.land/[email protected]/flags/mod.ts";
const defaultPort = 8085;
const flags = parse(Deno.args, {
string: ["port"],
alias: { port: "p" },
default: { port: "8085" },
});
const handler = async (request: Request): Promise<Response> => {
console.log("\n");
console.log(
`[%c${request.method}%c] %c${request.url}\n`,
"color: magenta; font-weight: bold;",
"",
"color: magenta;",
);
console.log("%c%o\n", "color: blue;", request.headers);
console.log("> %cBody", "color: cyan;");
console.log("%c%s\n", "color: cyan;", await request.text());
console.log("------------------------------------------------------------");
return new Response("", { status: 200 });
};
await serve(handler, { port: parseInt(flags.port) || defaultPort });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment