Fiber: 馃殌 New Function: QueryParser; separated from BodyParser

Created on 10 Jul 2020  路  8Comments  路  Source: gofiber/fiber

Is your feature request related to a problem?
No
Describe the solution you'd like
Currently, BodyParser does parse for both Request Body and Query String. If the Request with Query String has header content type of JSON, BodyParser will not parse.

Also to separate the parsers, I suggest to add a function QueryParser that will only for work Query Strings with any header content type irrespective of any HTTP Methods.

For e.g.
Case : POST Request with Filter options (Query Strings) in URL
In this case, BodyParser would parse the Request Body whereas QueryParser would parse the Query String

馃彿 Wait for Release

All 8 comments

Well, I liked it so in case we decide to implement it I believe that we should make a logic reverse this lib go-querystring

below I did put an example converting query string on a map

func (ctx *Ctx) QueryParser(out interface{}) error {
    var out map[string]string
    queries := c.Fasthttp.QueryArgs().String()
    out = map[string]string{}
    params := strings.Split(queries, "&")
    // TODO: Add recursive logic
    for _, part := range params {
        for k, r := range part {
            if r == '=' {
                out[part[:k]] = part[k+1:]
            }
        }
    }
    // TODO: Convert to struct
    // err := json.Unmarshal([]byte(queries), out)
    return nil
}
func myRoute(c *Fiber.Ctx) {
    var opt Options
    if err := c.QueryParser(opt); err != nil {
        c.SendStatus(fiber.StatusBadRequest)
        return
    }
    c.SendStatus(fiber.StatusOk)
}

@Fenny I can implement it, what do you think? I do make in this weekend.

We could use the existing code from ctx.go#L257

We could use the existing code from ctx.go#L257

@Fenny , I made a commit about this feature, please look it. Can I open a pull request?

https://github.com/gofiber/fiber/compare/master...renanbastos93:feat-queryparser

@renanbastos93 LGTM, feel free to open a PR!

@gofiber/maintainers, we will need to set a schedule when ctx,BodyParser will be deprecated

@gofiber/maintainers, we will need to set a schedule when ctx,BodyParser will be deprecated

@thomasvvugt
I suggest to deprecate it after that 3 minor.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ivan-Feofanov picture Ivan-Feofanov  路  3Comments

Terisback picture Terisback  路  3Comments

peterbourgon picture peterbourgon  路  4Comments

jeyraj picture jeyraj  路  4Comments

faultable picture faultable  路  3Comments