h2non/gentleman

HTTP method is overridden

solher opened this issue · 6 comments

I wanted to try gentleman doing a simple:

func (r *Repository) Send(authPayload, method, url string, data interface{}) ([]byte, error) {
    req := r.c.Request()
    req.AddHeader("Auth-Server-Payload", authPayload)
    req.Method(method)
    req.URL(url)
    req.JSON(data)
    res, err := req.Send()
    if err != nil {
        return nil, err
    }
        return res.Bytes(), nil
}

But when I use it, the HTTP method is overridden and the client always send a POST.

When I write fmt.Printf(req.Context.Request.Method) just before the Send and it prints a GET, I can see that a fmt.Printf(ctx.Request.Method) in the doDial method of the dispatcher prints a POST.

Am I doing something wrong ?

h2non commented

Well, that scenario could happen when you define a client level HTTP method.

I've fixed the issue. Try updating gentleman.

go get -u gopkg.in/h2non/gentleman.v0

Hum. It doesn't seems to solve the problem for me.

I don't define a client level HTTP method. I only do:

type Repository struct {
    c *gentleman.Client
}

func NewRepository() *Repository {
    return &Repository{c: gentleman.New()}
}

And then call the Send method.

h2non commented

Have you tried it again updating gentleman?. It should work now since the method is define either in client or request level via middleware call chain.

Yeah the update is done.

// Method defines the HTTP verb to be used.
func (r *Request) Method(method string) *Request {
    r.Middleware.UseRequest(func(ctx *context.Context, h context.Handler) {
        ctx.Request.Method = method
        fmt.Print(ctx.Request.Method)
        h.Next(ctx)
    })
    return r
}

Prints GET while the same in the doDial method still prints POST.

h2non commented

I see where is the issue. You're always calling JSON(), which implicitely defines the POST method if no method was previously defined. In that case you should consider calling that method only if payload data exists.

Oh ok ! Perfect, thanks a lot.