Post fails after multiple attempts #4

Closed
opened 2025-08-24 07:54:27 +00:00 by mybigman · 3 comments

As you can see first request all good, second request empty body, third request explodes.

go run main.go
Server running on http://localhost:8080
Raw body: {"key": "value", "number": 123}
Raw body:
panic: runtime error: slice bounds out of range [:-31]

goroutine 35 [running]:
io.ReadAll({0x126ef0048, 0x14000198000})
        /Users/champ/.gobrew/versions/1.24.5/go/src/io/io.go:713 +0x164
main.main.func2({0x1001c8c88, 0x14000198000})
        /Users/champ/Ultimate/projects/pelt/backend/main.go:18 +0x50
main.main.NewServer.func3({0x1001c8c88, 0x14000198000})
        /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:57 +0x44
git.urbach.dev/go/web.(*server).handleRequest(0x140000a6120, 0x14000198000, {0x1400019c0c0?, 0xe?}, {0x1400019c0c5?, 0x0?}, {0x126ef0028, 0x14000188010})
        /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:270 +0xf0
git.urbach.dev/go/web.(*server).handleConnection(0x140000a6120, {0x1001c8b28, 0x14000188010})
        /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:253 +0x258
created by git.urbach.dev/go/web.(*server).Run.func1 in goroutine 19
        /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:138 +0xa8
exit status 2
func main() {
	s := web.NewServer()

	s.Get("/", func(ctx web.Context) error {
		return ctx.String("Hello Get")
	})

	s.Post("/", func(ctx web.Context) error {
		bodyBytes, err := io.ReadAll(ctx.Request())
		if err != nil {
			return ctx.String("failed to read body: " + err.Error())
		}

		fmt.Println("Raw body:", string(bodyBytes))

		return ctx.String("Hello Post")
	})

	fmt.Println("Server running on http://localhost:8080")

	s.Run(":8080")
}
curl -v -X POST localhost:8080 -H "Content-Type: application/json" -d '{"key": "value", "number": 123}'
As you can see first request all good, second request empty body, third request explodes. ```go go run main.go Server running on http://localhost:8080 Raw body: {"key": "value", "number": 123} Raw body: panic: runtime error: slice bounds out of range [:-31] goroutine 35 [running]: io.ReadAll({0x126ef0048, 0x14000198000}) /Users/champ/.gobrew/versions/1.24.5/go/src/io/io.go:713 +0x164 main.main.func2({0x1001c8c88, 0x14000198000}) /Users/champ/Ultimate/projects/pelt/backend/main.go:18 +0x50 main.main.NewServer.func3({0x1001c8c88, 0x14000198000}) /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:57 +0x44 git.urbach.dev/go/web.(*server).handleRequest(0x140000a6120, 0x14000198000, {0x1400019c0c0?, 0xe?}, {0x1400019c0c5?, 0x0?}, {0x126ef0028, 0x14000188010}) /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:270 +0xf0 git.urbach.dev/go/web.(*server).handleConnection(0x140000a6120, {0x1001c8b28, 0x14000188010}) /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:253 +0x258 created by git.urbach.dev/go/web.(*server).Run.func1 in goroutine 19 /Users/champ/go/pkg/mod/git.urbach.dev/go/web@v0.0.0-20250722123400-9c3d8c620b57/Server.go:138 +0xa8 exit status 2 ``` ```go func main() { s := web.NewServer() s.Get("/", func(ctx web.Context) error { return ctx.String("Hello Get") }) s.Post("/", func(ctx web.Context) error { bodyBytes, err := io.ReadAll(ctx.Request()) if err != nil { return ctx.String("failed to read body: " + err.Error()) } fmt.Println("Raw body:", string(bodyBytes)) return ctx.String("Hello Post") }) fmt.Println("Server running on http://localhost:8080") s.Run(":8080") } ``` ```bash curl -v -X POST localhost:8080 -H "Content-Type: application/json" -d '{"key": "value", "number": 123}' ```
ed self-assigned this 2025-08-24 07:55:16 +00:00
Owner

Should be fixed in c50aa3983d.
Could you confirm it?

go get git.urbach.dev/go/web@c50aa3983d
Should be fixed in https://git.urbach.dev/go/web/commit/c50aa3983d877fae121c3ff99aa5703d75298398. Could you confirm it? ```shell go get git.urbach.dev/go/web@c50aa3983d ```
ed added spent time 2025-08-24 08:12:01 +00:00
15 minutes
Author

appears fixed mate... thanks

appears fixed mate... thanks
ed closed this issue 2025-08-24 09:24:37 +00:00
Author

Apologies mate whilst that issue is fixed theres something else. It doesnt receive the full body?

curl -v -X POST localhost:8080 -H "Content-Type: application/json" -d '{"name": "sample", "time": "Wed, 21 Oct 2015 18:27:50 GMT"'
Raw body: {"name": "sample", "time": "Wed
Apologies mate whilst that issue is fixed theres something else. It doesnt receive the full body? ```bash curl -v -X POST localhost:8080 -H "Content-Type: application/json" -d '{"name": "sample", "time": "Wed, 21 Oct 2015 18:27:50 GMT"' ``` ```go Raw body: {"name": "sample", "time": "Wed ```
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Total time spent: 15 minutes
ed
15 minutes
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: go/web#4
No description provided.