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

Document error? Maybe use ParsePath instead of Parse when parse path request #4527

Open
Yuelioi opened this issue Dec 26, 2024 · 3 comments
Open

Comments

@Yuelioi
Copy link

Yuelioi commented Dec 26, 2024

Description:

In the GoZero documentation, when parsing both body and path parameters simultaneously, it suggests using httpx.Parse for both. However, when doing so, the request body will be consumed during the first parse, leading to an EOF error when trying to parse the path parameters with httpx.Parse again.

The issue can be resolved by using httpx.ParsePath for parsing the path parameters, as it does not consume the request body.

Request Parameters | go-zero Documentation

Try parsing the request body and path parameters using httpx.Parse:

```go
var req Request
err := httpx.Parse(r, &req) // Parsing the body
```

Try parsing the path parameters after parsing the body:

```go
var pathReq Request
err = httpx.Parse(r, &pathReq) // This results in EOF error because the body was already consumed
```

Solution:

Instead of using httpx.Parse for both body and path parameters, use httpx.ParsePath for the path parameters, as it does not consume the body content:

var req Request
err := httpx.Parse(r, &req)  // Parse the body

var pathReq Request
err = httpx.ParsePath(r, &pathReq)  // Use ParsePath for path parameters, avoiding EOF
@Yuelioi Yuelioi changed the title Document error? Maybe use ParsePath instead of Parse Document error? Maybe use ParsePath instead of Parse when parse path request Dec 26, 2024
@kevwan
Copy link
Contributor

kevwan commented Dec 29, 2024

Why do you parse req twice? Body is a io.Reader, can only read once.

@Yuelioi
Copy link
Author

Yuelioi commented Dec 29, 2024

Why do you parse req twice? Body is a io.Reader, can only read once.

I need to get both the reqBody and pathReq at the same time.

@kevwan
Copy link
Contributor

kevwan commented Dec 30, 2024

Why do you parse req twice? Body is a io.Reader, can only read once.

I need to get both the reqBody and pathReq at the same time.

You can use httpx.Parse(...) to achieve this.

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

No branches or pull requests

2 participants