Skip to content

Conversation

@Akkadius
Copy link
Member

@Akkadius Akkadius commented Jul 8, 2025

I was working on a client project and needed the ability to print HTTP requests and found myself creating this quickly.

It works when environment variable HTTP_DEBUG is set, users can also set it through a setter method as well.

It works by wrapping your provided transport (or default transport)

client := &http.Client{Transport: godump.NewHttpDebugTransport(http.DefaultTransport)}

Before

image

After

image

Example Code

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"test/internal/godump"
)

func main() {
	// Prepare POST body as JSON
	payload := map[string]string{
		"title": "Test Issue from Go",
		"body":  "This is a test issue created using Go's net/http client.",
	}
	jsonBody, err := json.Marshal(payload)
	if err != nil {
		fmt.Println("Error marshaling JSON:", err)
		return
	}

	// Create POST request
	req, err := http.NewRequest("POST", "https://api.github.com/repos/goforj/godump/issues", bytes.NewBuffer(jsonBody))
	if err != nil {
		fmt.Println("Error creating request:", err)
		return
	}

	// Set headers
	req.Header.Set("Accept", "application/vnd.github.v3+json")
	req.Header.Set("Content-Type", "application/json")

	// Create HTTP client and send request
	client := &http.Client{Transport: godump.NewHttpDebugTransport(http.DefaultTransport)}
	resp, err := client.Do(req)
	if err != nil {
		fmt.Println("Error sending request:", err)
		return
	}
	defer resp.Body.Close()

	fmt.Println("Sent request to GitHub API")
}

@codecov
Copy link

codecov bot commented Jul 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@ccoVeille ccoVeille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have to rework it a bit

@Akkadius Akkadius force-pushed the akkadius/http-transport-dumper branch from 78248a6 to 0ded7cf Compare July 8, 2025 05:54
@Akkadius
Copy link
Member Author

Akkadius commented Jul 8, 2025

I may want to do some additional things to handle body printing differently

Comment on lines +134 to +138
// Add body as raw
body := strings.TrimSpace(bodyBuilder.String())
if body != "" {
payload["Body"] = body
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I would expect the body to be decoded as JSON in payload["Body_JSON"] it would help, having the body is not that useful, especially when dumped on one line with truncation

Suggested change
// Add body as raw
body := strings.TrimSpace(bodyBuilder.String())
if body != "" {
payload["Body"] = body
}
// Add body as raw
body := strings.TrimSpace(bodyBuilder.String())
if body != "" {
payload["Body"] = body
var rawJSON any
if err := json.Unmarshal(body, &rawJSON); err == nil {
payload["Body_JSON"] = rawJSON
}
}

Or maybe this

Suggested change
// Add body as raw
body := strings.TrimSpace(bodyBuilder.String())
if body != "" {
payload["Body"] = body
}
// Add body as raw
body := strings.TrimSpace(bodyBuilder.String())
if body != "" {
payload["Body"] = body
var rawJSON any
if err := json.Unmarshal(body, &rawJSON); err == nil {
payload["Body"] = rawJSON
}
}

Copy link
Member Author

@Akkadius Akkadius Jul 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code that is here doesn't make any sense. The last main remaining action item of this PR is to address body printing which I'll do at a later point. I'm not in a rush to merge this.

for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sort.Strings is somehow deprecated

Note: as of Go 1.22, this function simply calls slices.Sort.

use slices.Sort

Suggested change
sort.Strings(keys)
slices.Sort(keys)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants