Lib for parsing HTTP request path, parameters, and body

Hello everyone!

I have an idea to develop a package for parsing params needed to handle HTTP request when developing REST services. For example, the request (simplified):

PUT /user/1/?someGetParam=value

    "name": "Jack",
    "email": "Lala"

Params here are "1" (from the path), "value" (from GET), and body, encoded in JSON.

The library will parse that all into one struct, which you should define. The request bellow can be parsed into such struct:

type UpdateUserParams struct {
    ID      uint   `path:"id"`
    SomeGet string `get:"SomeGetParam"`
    Body    struct {
        Name string
        Email string
    } `body:"+"`

What do u think? Will u use it? What features u think not enough?

What if I have different path or query parameters? What if the JSON body has a different structure?

1 Like

You should define that structure yourself for every request handler, so you can use any fields you want

And how would your lib know which type to parse the request into?

You mean what field correspond to GET or PATH or Body? I think, struct tags will help: something like this:

type UpdateUserParams struct {
    ID uint `path:"id"`
    SomeGet string `get:"SomeGetParam"`
    Body struct {
        Name string
        Email string
    } `body:"+"`

No. How would the lib know that two requests must be mapped on different types? Consider two GET requests:

  • GET /foo/1?stuff=23
  • GET /bar/baz/12?answer=42

Would you library try to map both on the same type? If not, how would it decide which of two types to map the requests on?

I ask all this because I think HTTP requests can not easily be mapped on types. There is to much variability in them.

You should define two different structs.

type FooParams struct {
    SomeInt   uint
    Staff         int
type BarParams struct {
    AnotherInt   uint
    Answer      int

And then in handler call parser to parse with a request and that structure instance:

params := FooParams{}
parser.GetParser.Parse(r, &params)


params := BarParams{}
parser.GetParser.Parse(r, &params)

Do I have to do this manually?

I don’t see how such a lib would be an improvement over just using the methods of the request instance.

But who am I to tell you what to build? Just go ahead and later and build this library. If you have something to show, come back.

Yes, only manually

The main feauture here is checking arams corresponding to go types. If instead of having integer in path param ID, you have to make if in every request:

IDconverted, err := strconv.Atoi(
	pat.Param(r, "id"),
if err != nil {
	w.Write..(http.StatusBadRequest, err)...

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.