erikdubbelboer / fasthttp

This fork has been deprecated!
MIT License
50 stars 12 forks source link

Usage PostArgs() in client structure #17

Closed dgrr closed 6 years ago

dgrr commented 6 years ago

Do function does not consider Args of Request parameter.

I will try to fix it tomorrow.

erikdubbelboer commented 6 years ago

Can you explain what the bug is exactly?

dgrr commented 6 years ago
package main

import (
  "github.com/erikdubbelboer/fasthttp"
)

func main() {
  c := &fasthttp.Client{}

  req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
  req.Header.SetMethod("POST")
  req.SetRequestURI("...")

  args := req.PostArgs()
  args.Add("some", "value")

  c.Do(req, res)
}

This code makes POST request. I think that the error is that the PostArgs won't be used in POST request, because Do function does not consider postArgs var of Request structure. The correct way was the following:

package main

import (
  "github.com/erikdubbelboer/fasthttp"
)

func main() {
  c := &fasthttp.Client{}

  req, res := fasthttp.AcquireRequest(), fasthttp.AcquireResponse()
  req.Header.SetMethod("POST")
  req.SetRequestURI("...")

  args := req.PostArgs()
  args.Add("some", "value")

  req.SetBody(args.QueryString())

  c.Do(req, res)
}
erikdubbelboer commented 6 years ago

Ok seems like a good fix. Should I just merge that one commit or do you want to make a pull request for it (from a separate branch on your fork)?

Quons commented 2 years ago

If I want to modify the content in the body, I need to code as this?

req.SetBody([]byte("a=1")) // existing body
req.PostArgs().Add("b", "2") // add another value
req.SetBody(req.PostArgs().QueryString()) // reset the body