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

Request body is empty when GET request. #618

Closed
cooppor opened this issue Feb 21, 2023 · 1 comment
Closed

Request body is empty when GET request. #618

cooppor opened this issue Feb 21, 2023 · 1 comment

Comments

@cooppor
Copy link

cooppor commented Feb 21, 2023

When I use resty for the get request, I cannot parse the request body message cause it's empty. The follow codes can repeat the issue. By the way, if I change the both side request method as post, it works.

go.mod

go 1.18

require (
	github.com/gin-gonic/gin v1.8.2
	github.com/go-resty/resty/v2 v2.7.0
)

require (
	github.com/gin-contrib/sse v0.1.0 // indirect
	github.com/go-playground/locales v0.14.0 // indirect
	github.com/go-playground/universal-translator v0.18.0 // indirect
	github.com/go-playground/validator/v10 v10.11.1 // indirect
	github.com/goccy/go-json v0.9.11 // indirect
	github.com/json-iterator/go v1.1.12 // indirect
	github.com/leodido/go-urn v1.2.1 // indirect
	github.com/mattn/go-isatty v0.0.16 // indirect
	github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
	github.com/modern-go/reflect2 v1.0.2 // indirect
	github.com/pelletier/go-toml/v2 v2.0.6 // indirect
	github.com/ugorji/go/codec v1.2.7 // indirect
	golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
	golang.org/x/net v0.4.0 // indirect
	golang.org/x/sys v0.3.0 // indirect
	golang.org/x/text v0.5.0 // indirect
	google.golang.org/protobuf v1.28.1 // indirect
	gopkg.in/yaml.v2 v2.4.0 // indirect
)

main.go

package main

import (
	"github.com/gin-gonic/gin"
	"log"
	"net/http"
)

func main() {
	app := gin.Default()
	app.GET("/get-request-test", func(c *gin.Context) {
		var (
			body    map[string]any
			headers = make(map[string]string, 10)
		)
		if err := c.BindJSON(&body); err != nil {
			log.Printf("parse body failed: %v", err)
			return
		}
		headers["ContentType"] = c.GetHeader("Content-Type")
		headers["ClientID"] = c.GetHeader("Client-ID")
		headers["Authorization"] = c.GetHeader("Authorization")
		log.Println("Header: ")
		log.Printf("Content-Type		\t: %v", headers["ContentType"])
		log.Printf("Client-ID		\t: %v", headers["ClientID"])
		log.Printf("Authorization	\t: %v", headers["Authorization"])
		log.Println("Request Body: ")
		log.Printf("UserId	\t: %v", body["userId"])
		log.Printf("UserName	\t: %v", body["userName"])
		log.Printf("Address	\t: %v", body["address"])

		c.JSON(http.StatusOK, body)
	})

	err := app.Run(":8080")
	if err != nil {
		log.Fatal(err)
	}
}

resty_test.go

package main

import (
	"github.com/go-resty/resty/v2"
	"testing"
)

func Test_GetRequest(t *testing.T) {
	var cli = resty.New()
	resp, err := cli.R().
		SetHeader("Content-Type", "application/json").
		SetHeader("Client-ID", "20230221").
		SetHeader("Authorization", "Bearer 0123456789abcdefghijklmnopqrstuvwxyz").
		SetBody(map[string]any{
			"userId":   10001,
			"userName": "Michael",
			"address":  "Chaoyang District Beijing China",
		}).
		Get("http://localhost:8080/get-request-test")
	if err != nil {
		t.Fatalf("Request Failed, %v", err)
	}
	if resp.StatusCode() != 200 {
		t.Fatalf("Request Failed, Status Code %v", resp.StatusCode())
	}
	t.Logf("Response Body: %v", resp.String())
}

server log

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /get-request-test         --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8080
2023/02/21 17:09:51 parse body failed: EOF
[GIN] 2023/02/21 - 17:09:51 | 400 |    1.537488ms |       127.0.0.1 | GET      "/get-request-test"

test log

=== RUN   Test_GetRequest
    resty_test.go:24: Request Failed, Status Code 400
--- FAIL: Test_GetRequest (0.01s)

FAIL
@cooppor
Copy link
Author

cooppor commented Feb 21, 2023

this pr: Fix SetAllowGetMethodPayload option ignored for io.Reader payload inspired me, solved my confusion perfectly.
Respect!

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

No branches or pull requests

1 participant