Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bind Fails with Transfer-Encoding: chunked #2716

Closed
178inaba opened this issue Dec 10, 2024 · 0 comments · Fixed by #2717
Closed

Bind Fails with Transfer-Encoding: chunked #2716

178inaba opened this issue Dec 10, 2024 · 0 comments · Fixed by #2717

Comments

@178inaba
Copy link
Contributor

Issue Description

Bind fails when Transfer-Encoding: chunked is used.

Working code to debug

Server

package main

import (
	"fmt"
	"net/http"
	"net/http/httputil"

	"github.com/labstack/echo/v4"
)

type User struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	e := echo.New()
	e.POST("/", func(c echo.Context) error {
		req := c.Request()

		fmt.Println("----------")
		fmt.Println("ContentLength:", req.ContentLength)
		fmt.Println("TransferEncoding:", req.TransferEncoding)
		fmt.Println("----------")

		reqDump, err := httputil.DumpRequest(req, true)
		if err != nil {
			return err
		}
		fmt.Println(string(reqDump))

		var u User
		if err := c.Bind(&u); err != nil {
			return err
		}

		return c.JSON(http.StatusOK, u)
	})

	e.Logger.Fatal(e.Start(":1323"))
}
Log
----------
ContentLength: -1
TransferEncoding: [chunked]
----------
POST / HTTP/1.1
Host: localhost:1323
Transfer-Encoding: chunked
Accept-Encoding: gzip
Content-Type: application/json
User-Agent: Go-http-client/1.1

28
{"name":"foo","email":"foo@example.com"}
0

Client

package main

import (
	"bytes"
	"fmt"
	"io"
	"log"
	"net/http"
	"net/http/httputil"
)

func main() {
	req, err := http.NewRequest("POST", "http://localhost:1323", bytes.NewBufferString(`{"name":"foo","email":"foo@example.com"}`))
	if err != nil {
		log.Fatal(err)
	}
	req.Header.Set("Content-Type", "application/json")
	req.ContentLength = 0

	reqDump, err := httputil.DumpRequest(req, true)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(reqDump))

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Fatal(err)
	}
	defer resp.Body.Close()

	b, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("----------")
	fmt.Println(string(b))
}
Log
POST / HTTP/1.1
Host: localhost:1323
Content-Type: application/json

{"name":"foo","email":"foo@example.com"}
----------
{"name":"","email":""}

Version/commit

v4.13.0

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 a pull request may close this issue.

1 participant