@@ -48,6 +48,10 @@ func NewHandler(options HandlerOptions) *Handler {
48
48
mux .HandleFunc ("/event/{eventId}" , handler .getEventDetails )
49
49
mux .HandleFunc ("/events-sse" , handler .getEventsSSE )
50
50
51
+ // Download handlers
52
+ mux .HandleFunc ("/download/request-body/{eventId}" , handler .downloadRequestBody )
53
+ mux .HandleFunc ("/download/response-body/{eventId}" , handler .downloadResponseBody )
54
+
51
55
// Test routes
52
56
mux .HandleFunc ("/logs" , handler .getLogs )
53
57
mux .HandleFunc ("/http-client-requests" , handler .getHTTPClientRequests )
@@ -290,3 +294,99 @@ func truncate(s string, maxLen int) string {
290
294
}
291
295
return s [:maxLen ] + "..."
292
296
}
297
+
298
+ // downloadRequestBody handles downloading the request body for an event
299
+ func (h * Handler ) downloadRequestBody (w http.ResponseWriter , r * http.Request ) {
300
+ idStr := r .PathValue ("eventId" )
301
+ eventID , err := uuid .FromString (idStr )
302
+ if err != nil {
303
+ http .Error (w , "Invalid event id" , http .StatusBadRequest )
304
+ return
305
+ }
306
+
307
+ event , exists := h .eventCollector .GetEvent (eventID )
308
+ if ! exists {
309
+ http .Error (w , "Event not found" , http .StatusNotFound )
310
+ return
311
+ }
312
+
313
+ var body []byte
314
+ var contentType string
315
+
316
+ switch data := event .Data .(type ) {
317
+ case collector.HTTPRequest :
318
+ if data .RequestBody == nil {
319
+ http .Error (w , "No request body available" , http .StatusNotFound )
320
+ return
321
+ }
322
+ body = data .RequestBody .Bytes ()
323
+ contentType = data .RequestHeaders .Get ("Content-Type" )
324
+ case collector.HTTPServerRequest :
325
+ if data .RequestBody == nil {
326
+ http .Error (w , "No request body available" , http .StatusNotFound )
327
+ return
328
+ }
329
+ body = data .RequestBody .Bytes ()
330
+ contentType = data .RequestHeaders .Get ("Content-Type" )
331
+ default :
332
+ http .Error (w , "Event type does not have a request body" , http .StatusBadRequest )
333
+ return
334
+ }
335
+
336
+ if contentType == "" {
337
+ contentType = "application/octet-stream"
338
+ }
339
+
340
+ w .Header ().Set ("Content-Type" , contentType )
341
+ w .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=request-body-%s" , eventID ))
342
+ w .Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (body )))
343
+ w .Write (body )
344
+ }
345
+
346
+ // downloadResponseBody handles downloading the response body for an event
347
+ func (h * Handler ) downloadResponseBody (w http.ResponseWriter , r * http.Request ) {
348
+ idStr := r .PathValue ("eventId" )
349
+ eventID , err := uuid .FromString (idStr )
350
+ if err != nil {
351
+ http .Error (w , "Invalid event id" , http .StatusBadRequest )
352
+ return
353
+ }
354
+
355
+ event , exists := h .eventCollector .GetEvent (eventID )
356
+ if ! exists {
357
+ http .Error (w , "Event not found" , http .StatusNotFound )
358
+ return
359
+ }
360
+
361
+ var body []byte
362
+ var contentType string
363
+
364
+ switch data := event .Data .(type ) {
365
+ case collector.HTTPRequest :
366
+ if data .ResponseBody == nil {
367
+ http .Error (w , "No response body available" , http .StatusNotFound )
368
+ return
369
+ }
370
+ body = data .ResponseBody .Bytes ()
371
+ contentType = data .ResponseHeaders .Get ("Content-Type" )
372
+ case collector.HTTPServerRequest :
373
+ if data .ResponseBody == nil {
374
+ http .Error (w , "No response body available" , http .StatusNotFound )
375
+ return
376
+ }
377
+ body = data .ResponseBody .Bytes ()
378
+ contentType = data .ResponseHeaders .Get ("Content-Type" )
379
+ default :
380
+ http .Error (w , "Event type does not have a response body" , http .StatusBadRequest )
381
+ return
382
+ }
383
+
384
+ if contentType == "" {
385
+ contentType = "application/octet-stream"
386
+ }
387
+
388
+ w .Header ().Set ("Content-Type" , contentType )
389
+ w .Header ().Set ("Content-Disposition" , fmt .Sprintf ("attachment; filename=response-body-%s" , eventID ))
390
+ w .Header ().Set ("Content-Length" , fmt .Sprintf ("%d" , len (body )))
391
+ w .Write (body )
392
+ }
0 commit comments